Home > Backend Development > C++ > 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
Release: 2023-09-15 17:35:10
forward
1805 people have browsed it

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);
      }
   }
}
Copy after login

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 : &
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template