Non-blocking getch (), ncurses

I'm having some problems getting ncurses' getch () to block. Does the default work seem non-blocking (or did I skip some initialization)? I would like it to work as getch () on Windows. I tried different versions

timeout(3000000); nocbreak(); cbreak(); noraw(); etc... 

(not all at the same time). I would prefer (explicitly) to use any WINDOW , if possible. A while loop around getch (), checking for a specific return value is also OK.

+14
linux g ++ ncurses getch blocking
May 25 '09 at 1:38
source share
3 answers

The curses library is a batch deal. You cannot just pull out one routine and hope for the best without initializing the library. Here is the code that correctly blocks on getch() :

 #include <curses.h> int main(void) { initscr(); timeout(-1); int c = getch(); endwin(); printf ("%d %c\n", c, c); return 0; } 
+30
May 25 '09 at 2:11 a.m.
source share

You need to call initscr() or newterm() to initialize the curse before it works. This works fine for me:

 #include <ncurses.h> int main() { WINDOW *w; char c; w = initscr(); timeout(3000); c = getch(); endwin(); printf("received %c (%d)\n", c, (int) c); } 
+7
May 25 '09 at 2:08 a.m.
source share

From the man page (highlighted by me):

The timeout and wtimeout block or non-block reads for a given window. If delay negative, read blocking is used (i.e., it waits indefinitely for input).

+6
May 25 '09 at 1:44
source share



All Articles