如何在 C++ (UNIX) 中完全阻止用户输入

How to block user input entirely in C++ (UNIX)

本文关键字:输入 用户 C++ UNIX      更新时间:2023-10-16

我正在做一个项目,我想在 UNIX 上C++输出有延迟。以下是我所说的一个例子:

cout << "You walk through the darkness, no sign of life in sight" << endl;
usleep(1000000);
cout << "What would you like to do?" << endl;
cin >> userCommand;

现在,如果用户在 usleep 进行时键入某些内容,它稍后会进入 cin 语句,虽然我可以给出适当的错误消息,但我宁愿不处理。此外,有时我希望屏幕上的某些内容,因此我不希望用户只按住 Enter 并清除屏幕。

是否有任何命令可以完全阻止用户输入甚至与屏幕交互?我可以在我的 cin 语句之前停用并在 cin 语句之后重新激活的东西?

谢谢!

您可以使用

#include <stdio.h>
...
cout << "You walk through the darkness, no sign of life in sight" << endl;
getch();
cout << "What would you like to do?" << endl;
cin >> userCommand;

getch(( 块,直到你按下一个键