我正在使用 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/