侦测按键事件可以用C++的conio.h头文件,用到的函数:
_CRTIMP int __cdecl _getch(void);
输入以下代码:
#include <iostream>
#include <conio.h>
using namespace std;int main() {char key;while (true) {cout << "Enter a key: ";key = _getch();printf("%c, %d\n", key, key);}return 0;
}
这个程序可以输出按键的字符和整数值。
运行结果:
运行结果中的“ , 32”输入的是空格,“, 13”输入的是回车。