偵測按鍵事件可以用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”輸入的是回車。