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() 不是标准库。因此,我们应在代码中避免使用它。

更新时间: 2019 年 7 月 30 日

2K+ 浏览量

开启您的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.