The kbhit function does not take the character out of the buffer (44895)



The information in this article applies to:

  • The C Run-Time (CRT), when used with:
    • Microsoft C for MS-DOS
    • Microsoft C for OS/2
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0
    • Microsoft Visual C++ 2005 Express Edition
    • Microsoft Visual C++ .NET (2003)
    • Microsoft Visual C++ .NET (2002)

This article was previously published under Q44895
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model..

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
}
				

Modification Type:MajorLast Reviewed:1/5/2006
Keywords:kbcode kbinfo KB44895 kbAudDeveloper