### _kbhit -- Test for Keystroke

Format

>     include <conio.h>
>     int _kbhit(void);

Language Level: Extension
_kbhit tests if a key has been pressed on the keyboard. If the result is nonzero, a keystroke is waiting in the buffer. You can read in the keystroke using the _getch or _getche function. By calling _kbhit prior to calling _getch or _getche, you can prevent your program from waiting for a keyboard input.

Return Value
_kbhit returns a nonzero value if a key has been pressed. Otherwise, it returns 0.

Example
This example uses _kbhit to test for the pressing of a key on the keyboard and to print a statement with the test result.

    /************************************************************************
       This example uses _kbhit to test for the pressing of a key on the
       keyboard and to print a statement with the test result.
    ************************************************************************/

    #include <conio.h>
    #include <stdio.h>

    int main(void)
    {
       int ch;

       printf("Type in some letters.\n");
       printf("If you type in an 'x', the program ends.\n");

       for (; ; ) {
          while (0==_kbhit()){
          /*
             .
             .
             .
             Insert code here. This code will be executed until
             the user presses a key.
             .
             .
             .
                                                                    */
          }

          ch = _getch();
          printf("You have pressed the '%c' key.\n",ch);

          if ('x' == ch)
             break;
        }
        return 0;

        /*******************************************************************
           The output should be similar to:

           Type in some letters.
           If you type in an 'x', the program ends.
           You have pressed the 'f' key.
           You have pressed the 'e' key.
           You have pressed the 'l' key.
           You have pressed the 'i' key.
           You have pressed the 'x' key.
        *******************************************************************/
     }

_getch - _getche -- Read Character from Keyboard
<conio.h>