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++ 中直接从键盘读取输入?
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
