Results 1 to 5 of 5

Thread: C : Use of kbhit()

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    C : Use of kbhit()

    Hello, How are you all? I am the student of the MscIT. I have the knowledge of the C language. I want to know about the kbhit() function of the C language. I also want to know about the For what purpose the kbhit() function can be used and how they can be initialized or invoked. So, Anyone is there who can help me to know about the kbhit() function? As soon as possible provide me the solution. i am waiting for your reply!!!!!!!!!

  2. #2
    Join Date
    May 2008
    Posts
    2,393

    Use of kbhit()

    To check or test the keystroke that whether the keystrokes are currently available or not the kbhit() function can be used. To obtain the keystroke getch(0 or getche(0 function can be used, whenever the one of the keystroke can be available. Until a keystroke is available the kbhit() function can be called continuously with a stand-alone program. You have to note that loops involving the kbhit() function can not be recommended in multitasking systems. The following is the syntax of the kbhit() function :
    #include <conio.h>
    int kbhit( void );

  3. #3
    Join Date
    Feb 2008
    Posts
    1,856

    C : Use of kbhit()

    The following sample program describes you about the kbhit() function :
    #include <stdio.h>
    #include <conio.h>
    int main(void)
    {
    double amt;
    amt = 0.20;
    cprintf(" Printing 6-percent tax table\n\r ");
    cprintf(" Press a key to stop.\n\n\r ");
    do {
    cprintf(" Amount : %f, Tax : %f\n\r ", amount, amount * 0.06);
    if(kbhit())
    break;
    amt = amt + 0.40;
    } while(amt < 200.0);
    return 0;
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    kbhit() function : Program

    The following program demonstrate that program loops until a any key is pressed or a count can be exceeded :
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
    unsigned long j;
    printf( " Program looping. Press any key.\n " );
    for( j = 0; j < 20000; j++ ) {
    if( kbhit() ) {
    getch();
    break;
    }
    }
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    1,949

    Re: C : Use of kbhit()

    Sample Example :
    #include <conio.h>
    #include <stdio.h>
    int main( void )
    {
    while( !_kbhit() )
    _cputs( "Hit me!! " );
    printf( "\nKey struck was '%c'\n", _getch() );
    }
    Output :
    Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!! Hit me!!
    Key struck was 'q'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •