c - C中的Escape键无限循环

标签 c infinite-loop ncurses

我正在使用 curses 库。我希望我的应用程序继续执行直到按下 Escape 键。它不应该阻止下一个键输入,

我的工作代码是: 在 key=getch() 处等待用户输入;我希望这是非阻塞的,直到有人按下 ESCAPE KEY。 它不应该等待键盘输入。

while (true)
{
    key=getch();
    if (key==ESCAPE)
    {
        break;
    }
    //else
    //{
        //execute something 
    //}
}//while end

最佳答案

这是一个示例:

#include <curses.h>

int
main (void)
{
  int c;

  initscr ();
  noecho ();
  timeout (0);

  while (1)
    {
      c = getch ();
      if (c != ERR)
      {
        printw ("%c", c);
      }
      if (c == 27)
      {
        break;
      }
    }

  echo ();
  endwin ();
  return 0;
}

timeout () 函数告诉阻塞调用在返回之前应该等待多长时间。 0 值表示非阻塞,正值 x 将在返回前等待 x 毫秒。

这是来自 curses 手册页的部分:

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). If delay is zero, then non- blocking read is used (i.e., read returns ERR if no input is waiting). If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input. Hence, these rou‐ tines provide the same functionality as nodelay, plus the additional capability of being able to block for only delay milliseconds (where delay is positive).

关于c - C中的Escape键无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846738/

相关文章:

c - Scanf 在 C 中有空格

c - 刷新行为 (nCurses)

c - Ncurses 滚动行

macos - 如何在 os x、centos 6 上构建 goncurses

C 指针转换 - 值截断

c - 为什么这些构造使用增量前和增量后未定义的行为?

Python:Ctypes如何检查内存管理

javascript - 为什么距离的这种指数衰减会导致 99 [精确] 的一次性误差?

python - 键盘中断不会停止我的翻译

c++ - 尝试 Box Translation 结果挂起输出?