在vs中可以使用_kbhit()函數來獲取鍵盤事件,使用時需要加入conio.h頭文件,例:
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int ch;
while (1){
if (_kbhit()){//如果有按鍵按下,則_kbhit()函數返回真
ch = _getch();//使用_getch()函數獲取按下的鍵值
cout << ch;
if (ch == 27){ break; }//當按下ESC時循環,ESC鍵的鍵值時27.
}
}
system("pause");
}
鍵盤的鍵值是遵循ASCII碼碼錶的,對應鍵值如下: