时间:2021-03-08 11:38:57 | 栏目:C代码 | 点击:次
在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码码表的,对应键值如下:



