ホームページ  >  記事  >  バックエンド開発  >  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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。