C++ kbhit()函数和 getch()函数配套解读

1. 功能及返回值

  • kbhit()函数检测键盘是否有键按下,如果有键按下,则返回对应键值(ASCII码值);否则返回零,kbhit不等待键盘按键,无论有无按键都会立即返回 
  • getch()函数从键盘读入一个字符,返回字符的 ASCII 码值
#include 
#include 

using namespace std;

int main()
{
	/* Display message until key is pressed */
	while (!kbhit())
	{
		cout << "Hit me!   ";
	}

	cout << "/nkey struck was " << getch() << endl;
		
	
	return 0;
}

没有键盘按下,那么 while 循环一直执行,当有键盘按下,那么就会跳出 while 循环,然后输出按下的 ASCII 码

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