INFO: kbhit() Does Not Take Character out of Buffer

ID: Q44895


The information in this article applies to:
  • The C Run-Time (CRT), included with:
    • Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, 6.0ax
    • Microsoft C for OS/2, versions 5.1, 6.0, 6.0a
    • Microsoft C/C++ for MS-DOS
    • Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5
    • Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 4.0, 5.0, 6.0


SUMMARY

When an application tests the value returned by the kbhit() function, the application must explicitly remove the character from the keyboard buffer after entering the keystroke. Otherwise, kbhit() continues to return TRUE.

The following code example demonstrates the situation:

Sample Code


/*
 * Compiler options needed: None
 */ 

#include <conio.h>

void main(void)
{
   while (!kbhit())
      ;          // Waits for keystroke
   while (kbhit())
      getch();   // Empties buffer
   while (!kbhit())
      ;          // Waits for keystroke
} 

Additional query words:

Keywords : kbcode kbCRT kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600
Version : winnt:1.0,2.0,4.0,5.0,6.0
Platform : winnt
Issue type : kbinfo


Last Reviewed: July 24, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.