How to Use kbhit() Function in C Programming Language

kbhit() Function in C Programming Language

Introduction :

To create responsive and user-friendly console and graphics applications, you have to handle keyboard input effectively. In the C or C++ programming language, sometime you have to capture keyboard input in a non-blocking way.

The kbhit() function in the C or C++ programming language, is very useful for handling or capturing keyboard input dynamically in a non-blocking manner. It allows you to check if a key has been pressed or not without waiting for the user to press the “Enter” key. This feature is particularly valuable when you create real-time console or graphics applications that respond to user input.

In this article, I shall show you how to use kbhit() function of conio.h library of the Turbo C++ IDE using C or C++ programming language. Here, I show you how to kbhit() function in console application. You can use kbhit() function in a graphics program such as clock, traffic, solar system or other project in my site.

What is kbhit() function :

The kbhit() function is not a standard library function of C or C++ programming language. It is a function of conio.h library in Turbo C++ IDE. The function name “kbhit” stands for “keyboard hit”. It is commonly used to determine if a key has been pressed or not in a non-blocking manner. It is generally used in console-based or graphics-based programs where you can do some actions without press “Enter” key.

Unlike getch() function, kbhit() doesn’t block the execution of the program to take user input. It is generally used where needs to continue the program and sometimes check for user input. You can use kbhit() function in a loop to continuously check keyboard input and respond to key presses in real-time.

You can use kbhit() function in various dynamic console or graphics programs such reading a single key, detecting arrow keys, interactive console menus, simple text-based game, different type of graphics programs or any program where you need to react to user input in real-time without waiting for the user to press “Enter” key.

Syntax of kbhit() function :

The following code is the syntax of kbhit() function in C or C++ programming language.

int kbhit(void);

The kbhit() function does not take any parameter or argument. The return value of kbhit() function is an integer. It typically returns a non-zero (true) value if a key has been pressed means input is available. It returns zero (false) if no key has been pressed means there is no input.

About the program :

This is a simple program where I show you how you can use kbhit() function by C or C++ programming language. In the program, you have to input character one by one and it displays the character which you have pressed. When you press the “Q” or “q” key, you can see a stopping message which waits for 2 seconds before stopping the program. The output of the program will depend on the user’s input.

Explanation of the program :

First, you have to include necessary header file or library in the program. So, you can include stdio.h library for printf() function for writing to the console. To use kbhit() and getch() functions for interacting with the keyboard, you can include conio.h library. You have to also include dos.h library for delay() function to introduce a time delay.

Inside the main function, declares a character variable “ch_input” that stores the user input. In the while loop, I have used kbhit() function to check a keyboard key is pressed. The kbhit() function returns a non-zero value (true) when a key is pressed. Using getch() function, you can read the key without echoing it and stored in the “ch_input” variable.

After that, in the “if” statement check the key is “q” or “Q”. If the input character is “q” or “Q”, you can see a stopping message on the console using printf() function. Using delay() function, introduce a delay to see the output properly on the screen. Then breaks out of the while loop using “break” statement and stop the program. If the input character is not “q” or “Q”, display that character on the screen using printf() function.

How run the program :

To run the program, you have to install the Turbo C++ IDE. After installing the Turbo C++, open it. Next, create a C or C++ file with .c or .cpp extension in Turbo C++ IDE. Now, copy the below code of the program and paste into your C or C++ file which you have just created. If you do not know how to copy paste in the Turbo C++ IDE, click here. You can also see my other post to know how to use graphics.h in Turbo C++ IDE.

Source code :

The following code is used for kbhit() function by C or C++ programming language.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main()
{
    char ch_input;
    while (!kbhit())
    {
        ch_input = getch();
        if (ch_input == 'q' || ch_input == 'Q')
        {
            printf("Your input is %c so the program is stoped now\n", ch_input);
            delay(2000);
            break;
        }
        printf("Your input is %c\n", ch_input);
    }
    return 0;
}

Output :

When you run the program, you can see the output of the program of kbhit() function in the C or C++ programming language.

output of the kbhit() function

Conclusion :

Finally, you have learned how to use kbhit() function in the C or C++ programming language. Using kbhit() function, you can create various applications that listen for keystrokes while performing other tasks of the program. Thank you for visiting my site.

Scroll to Top