How do I port this program from conio to curses?
Asked Answered
L

1

9

I wrote this simple program on Windows. Since Windows has conio, it worked just fine.

#include <stdio.h>
#include <conio.h>

int main()
{
    char input;

    for(;;)
    {
        if(kbhit())
        {
            input = getch();
            printf("%c", input);
        }
    }
}    

Now I want to port it to Linux, and curses/ncurses seems like the right way to do it. How would I accomplish the same using those libraries in place of conio?

Loquitur answered 3/9, 2012 at 12:13 Comment(0)
Z
12
#include <stdio.h>
#include <ncurses.h>

int main(int argc, char *argv)
{
    char input;

    initscr(); // entering ncurses mode
    raw();     // CTRL-C and others do not generate signals
    noecho();  // pressed symbols wont be printed to screen
    cbreak();  // disable line buffering
    while (1) {
        erase();
        mvprintw(1,0, "Enter symbol, please");
        input = getch();
        mvprintw(2,0, "You have entered %c", input);
        getch(); // press any key to continue
    }
    endwin(); // leaving ncurses mode    
    return 0;
}

When building your program do not forget to link with ncurses lib (-L lncurses) flag to gcc

gcc -g -o sample sample.c -L lncurses

And here you can see kbhit() implementation for linux.

Zipangu answered 3/9, 2012 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.