How to Empty (Flush) the Keyboard Buffer in Basic (58213)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0

This article was previously published under Q58213

SUMMARY

The two programs below show two different methods of emptying the keyboard input buffer. The first program uses a DOS interrupt to clear the buffer, while the second program uses the INKEY$ function.

This information applies to Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50 for MS-DOS; to Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS; and to Microsoft Basic Professional Development System (PDS) Version 7.00 for MS-DOS.

MORE INFORMATION

Code Example 1

The following program uses DOS interrupt 21 hex (33 decimal) with function 0C hex (12 decimal), which flushes the input (type-ahead) keyboard buffer. To run this program inside the QuickBasic (QB.EXE) environment, you must load the Quick library QB.QLB with the /L option. To run this program inside the QuickBasic Extended (QBX.EXE) environment supplied with Microsoft Basic PDS Version 7.00, load the Quick library QBX.QLB with the /L option.
   'For Basic PDS Version 7.00 you must instead use $INCLUDE: 'QBX.BI'
   REM $INCLUDE: 'qb.bi'
   DIM inregs AS regtype
   DIM outregs AS regtype
   inregs.ax = &HC00   ' 0C hex goes in AH, high byte of AX register
                       ' 00 goes in AL, the low byte of AX register
   CALL INTERRUPT(&H21, inregs, outregs)
   END
				

Code Example 2

The following program uses the INKEY$ function to read characters from the keyboard buffer until no more are available. This program can also be used under MS OS/2 protected mode when compiled with Microsoft Basic Compiler Versions 6.00 and 6.00b and Microsoft Basic PDS Version 7.00.
   WHILE INKEY$ <> ""
   WEND
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB58213