29

我在阻止 ncurses 的 getch() 时遇到了一些问题。默认操作似乎是非阻塞的(或者我错过了一些初始化)?我希望它像 Windows 中的 getch() 一样工作。我试过各种版本的

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

(不是同时)。如果可能的话,我宁愿不(明确)使用 any WINDOWwhile围绕 getch() 循环,检查特定的返回值也可以。

4

3 回答 3

41

curses 库是一揽子交易。如果没有正确初始化库,您不能只抽出一个例程并希望获得最好的结果。这是一个正确阻止的代码getch()

#include <curses.h>

int main(void) {
  initscr();
  timeout(-1);
  int c = getch();
  endwin();
  printf ("%d %c\n", c, c);
  return 0;
}
于 2009-05-25T02:11:53.743 回答
11

手册页(强调添加):

timeoutand例程为wtimeout给定窗口设置阻塞或非阻塞读取。如果delay为负,则使用阻塞读取(即,无限期地等待输入)。

于 2009-05-25T01:44:02.037 回答
10

你需要调用initscr()newterm()初始化 curses 才能工作。这对我来说很好:

#include <ncurses.h>

int main() {
    WINDOW *w;
    char c;

    w = initscr();
    timeout(3000);
    c = getch();
    endwin();

    printf("received %c (%d)\n", c, (int) c);
}
于 2009-05-25T02:08:07.243 回答