首頁  >  文章  >  後端開發  >  在C語言中,kbhit指的是偵測鍵盤是否有輸入的函數

在C語言中,kbhit指的是偵測鍵盤是否有輸入的函數

WBOY
WBOY轉載
2023-09-15 17:35:101609瀏覽

在C語言中,kbhit指的是偵測鍵盤是否有輸入的函數

在這裡,我們將看到 C 中的 kbhit 功能。 kbhit 基本上就是鍵盤敲擊。此函數位於 conio.h 頭檔中。因此,為了使用它,我們必須將此頭檔包含到我們的程式碼中。

kbhit() 的功能是,按下某個鍵時,它會傳回非零值,否則傳回零。

範例

#include <stdio.h>
#include <conio.h>
main() {
   char ch;
   printf("Enter keys (ESC to exit)</p><p>");
   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</p><p>", 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() 不是標準函式庫。所以我們應該在程式碼中避免這種情況。

以上是在C語言中,kbhit指的是偵測鍵盤是否有輸入的函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除