C Language Development Kit/Functions/KbHit

From OptiWiki

Jump to: navigation, search
Description This function checks to see if a keystroke is available.
Syntax
unsigned int kbhit(void);
Arguments None
Returns When a keystroke is available, kbhit() returns a non zero integer, zero is returned otherwise.
Remarks Use the idle() function when kbhit()

is used to wait for keyboard input. The setecho() function controls displaying typed characters in cooperation with kbhit(). When a user wants to wait for a key press, it is advised to call resetkey() first, in order to be sure that a previous key, still in the keyboard buffer, causes kbhit() to return with TRUE. Note: kbhit() is not a good method to wait for shifted characters, because kbhit() will return before the character is completed. For shifted characters it is better to use the construction: int key; while( (key = getchar()) == EOF) idle();

Example
#include "lib.h"
#include <stdio.h>
 
void main( void )
{
    int x, y;
 
    while( 1 )
    {
        printf("\f");
 
        for( x=0; x < 96; x++)
            drawline( x, 0, 50, 24, 1);
 
        for( y=0; y < 48; y++)
            drawline( 96, y, 50, 24, 1);
 
        for( x=96; x >= 0; x\-\-)
            drawline( x, 47, 50, 24, 1);
 
        for( y=47; y >= 0; y\-\-)
            drawline( 0, y, 50, 24, 1);
 
        gotoxy( 3, 2);
        printf("Press key");
        while(!kbhit())
            idle();
        resetkey();
    }
}


Views
Personal tools