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+ 瀏覽量

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告