Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking getch(), ncurses

I'm having some problems getting ncurses' getch() to block. Default operation seems to be non-blocking (or have I missed some initialization)? I would like it to work like getch() in Windows. I have tried various versions of

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

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

like image 539
Jonas Byström Avatar asked May 25 '09 01:05

Jonas Byström


People also ask

What is Ncurses library Linux?

Ncurses is a programming library providing an API, allowing the programmer to write text-based user interfaces, Text User Interface (TUI), in a terminal-independent manner. It also optimizes screen changes, in order to reduce the latency experienced when using remote Unix shell.


1 Answers

The curses library is a package deal. You can't just pull out one routine and hope for the best without properly initializing the library. Here's a 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; } 
like image 190
Norman Ramsey Avatar answered Sep 23 '22 01:09

Norman Ramsey