Here we will see the kbhit functionality in C. The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code.
The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero.
Example
#include <stdio.h> #include <conio.h> main() { char ch; printf("Enter keys (ESC to exit)\n"); 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\n", 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 in our code.