Windows 系统下的 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");
}
相关阅读