这个问题我想了很多次,但总是忘记问。那么我如何编写一个 C/C++ 例程来检查我按下的键,以便在按下该键后立即返回结果?如何在 Windows 中实现这一点?如何在 Linux 中实现这一点?如果我使用 Dev-C++ 和 Code::Blocks ,我应该包含哪些 header ?
我试试这个:
#include <iostream>
#include <conio.h>
int main(void)
{
using namespace std ;
cout << "Press a key" << endl ;
char key ;
getch() >> key ;
cout << "\nYou have pressed: " << key << endl ;
return 0 ;
}
但它没有告诉我按了什么键。如何获取键值?
最佳答案
您可以使用getch()
来自 <conio.h>
的函数,例如:
int c;
c = getch();
还有一个GetAsyncKeyState
功能:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx您可能会发现有用。
关于c++ - 如何编写一个 C++ 例程来检查我按下了键盘上的哪个键,以便我可以单独处理每个按键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709072/