Non-blocking getch(), ncurses

后端 未结 3 593
轻奢々
轻奢々 2020-12-03 01:06

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

相关标签:
3条回答
  • 2020-12-03 01:38

    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;
    }
    
    0 讨论(0)
  • 2020-12-03 01:54

    From a man page (emphasis added):

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

    0 讨论(0)
  • 2020-12-03 01:55

    You need to call initscr() or newterm() to initialize curses before it will work. 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);
    }
    
    0 讨论(0)
提交回复
热议问题