C++/C--Windows下获取键盘事件

在Visual Studio 2013中可以使用_kbhit()函数来获取键盘事件,使用时需要加入conio.h头文件,例:

#include 
#include 
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");
	return 0;
}

键盘的键值是遵循ASCII码码表的,对应键值如下:

转载自:
博主:lindorx
博文地址:https://blog.csdn.net/lindorx/article/details/78760610
来源:CSDN

你可能感兴趣的:(C/C++)