如何在有限的时间内等待键盘输入
C++ How to wait for keyboard input for a limited time
我希望我的控制台应用程序停止并等待用户按下一个键。如果在限定时间后没有键盘输入,假设是2秒,执行将恢复。
我知道没有解决方案可以在所有实现中移植,但是至少有简单的解决方案吗?
像这篇或这篇这样的文章处理类似的问题,但他们似乎没有提供一个具体问题的答案。
如果我可以使用护士(我不能),我会使用getch()
和timeout()
函数做这样的事情:
#include <ncurses.h>
int main()
{
initscr(); // Start curses mode
cbreak(); // Turn off line buffering
timeout(2000); // Wait 2000 ms for user input
while (true) {
addstr("You have 2 sec. to press 'q', otherwise ...n");
refresh();
int inKey = getch();
if (inKey == 'q') break;
}
endwin();
return 0;
}
这样做的一种方法是使用低级read()(假设您在Posix上)与超时信号耦合在一起。由于read()将在信号处理后以EINTR退出,因此您将知道已达到超时。
相关文章:
- 物理键盘上的触发器按键
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 如何使用C/C++在MacOSX中获得键盘布局
- 如何让LLDB在成功时退出,在失败时等待
- 等待整个 omp 块完成,然后再调用第二个函数
- 视窗键盘输入 c++
- 提升 ASIO - io_service 不要等待连接到线程
- 如何在C++中实现带有packaged_task的异步等待循环?
- 虚假唤醒是否会解锁所有等待线程,甚至是不相关的线程?
- 如何等待窗口隐藏在Qt中?
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- C ++异步键盘输入(标准方式)
- 键盘不起作用 - Arduino Uno - 与变量有关的多个错误
- 如何在不等待检索的情况下获取C++中的内存位置?
- 等待被迷住了,没有回来
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- 如何让程序等待键盘输入
- 如何在Qt(c++)中等待键盘输入
- 如何在有限的时间内等待键盘输入
- 如何等待键盘输入之前清除屏幕(getchar不工作)