Is there a way to read input directly from the keyboard in standard C++?(有没有办法在标准 C++ 中直接从键盘读取输入?)
问题描述
我知道有 std::cin
,但这需要用户输入一个字符串,然后按 ENTER.有没有一种方法可以简单地获取按下的下一个键而无需按 ENTER 确认
And I know there's std::cin
, but that requires the user to enter a string, then press ENTER. Is there a way to simply get the next key that is pushed without needing to press ENTER to confirm
推荐答案
可以使用
#include <conio.h>
然后用这种情况捕获字符
and then catch char with cases such as this
char c;
if (_kbhit())
{
c = getch();
switch(c)
{
case ‘ H’ :
cout << "up arrow key!" << endl;
break;
}
}
注意:我没试过……记得把整个东西放到while(true)"中测试一下.
Beware: I have not tried it... and remember to put the whole thing into a "while(true)" to test.
这篇关于有没有办法在标准 C++ 中直接从键盘读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在标准 C++ 中直接从键盘读取输入?


- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07