C语言中的kbhit

kbhit()出现在conio中。h,用于确定是否按下了某个键。要在程序中使用kbhit函数,应该包含头文件“conio.h”。如果按下某个键,则返回非零值,否则返回零。

null

// C++ program to demonstrate use of kbhit()
#include <iostream.h>
#include <conio.h>
int main()
{
while (!kbhit())
printf ( "Press a key" );
return 0;
}


输出:

"Press a key" will keep printing on the 
console until the user presses a key on the keyboard.

注: kbhit()不是标准的库函数,应该避免使用。

使用kbhit获取按下的键的程序

// C++ program to fetch key pressed using
// kbhit()
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char ch;
while (1) {
if ( kbhit() ) {
// Stores the pressed key in ch
ch = getch();
// Terminates the loop
// when escape is pressed
if ( int (ch) == 27)
break ;
cout << "Key pressed= " << ch;
}
}
return 0;
}


输出:

Prints all the keys that will be pressed on
 the keyboard until the user presses Escape key

本文由 西周星1号 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享