C 语言中的 kbhit
本文将介绍 C 中的 kbhit 函数。kbhit 的基本含义是键盘击键。此函数位于 conio.h 头文件中。因此,要使用此函数,我们必须将该头文件包含到代码中。
kbhit() 函数的功能是:在按下按键时返回非零值,否则返回零。
示例
#include <stdio.h>
#include <conio.h>
main() {
char ch;
printf("Enter keys (ESC to exit)
");
while (1) { //define infinite loop for taking keys
if (kbhit) {
ch = getch(); // Get typed character into ch
if ((int)ch == 27) //when esc button is pressed, then it will comeout from loop
break;
printf("You have entered : %c
", ch);
}
}
}输出
Enter keys (ESC to exit) You have entered : i You have entered : t You have entered : D You have entered : w You have entered : 5 You have entered : / You have entered : * You have entered : + You have entered : You have entered : o You have entered : You have entered : &
注意:kbhit() 不是标准库。因此,我们应在代码中避免使用它。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP