- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
kbhit in C language
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)
"); 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
", 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.
Advertisements