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.
#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 : &
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!