Home  >  Article  >  Backend Development  >  In C language, kbhit refers to the function that detects whether there is input on the keyboard.

In C language, kbhit refers to the function that detects whether there is input on the keyboard.

WBOY
WBOYforward
2023-09-15 17:35:101608browse

In C language, kbhit refers to the function that detects whether there is input on the keyboard.

Here we will see the kbhit function in C. A kbhit is basically a keyboard hit. This function is located in the conio.h header file. Therefore, in order to use it, we must include this header file into our code.

The function of kbhit() is that when a key is pressed, it returns a non-zero value, otherwise it returns zero.

Example

#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);
      }
   }
}

Output

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 : &

Note: This kbhit() is not a standard library. So we should avoid this situation in our code.

The above is the detailed content of In C language, kbhit refers to the function that detects whether there is input on the keyboard.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete