我正在定时循环中接收输入,即使循环已经结束,getch()也会阻止(等待键)程序。当使用 timeout() 或其他函数时,它不会阻塞,而是终止整个程序。
initscr();
raw();
noecho();
while(time(NULL)-initTime < maxtime)
{
timeout(3); // 3 as an example
getch();
}
// do stuff
refresh();
// hit a key to exit
getch();
endwin();
这似乎没什么用,所以我一定错过了一些东西。如何非阻塞(例如在 3 秒内)并继续执行我的代码?
正如评论中所述,问题在于仅设置
timeout
一次,从而将其应用于整个程序。
解决方案:
while(time(NULL)-initTime < maxtime)
{
timeout(3); // 3 as an example
getch();
}
timeout(-1); // The rest of the getch() calls can block now