[comp.sys.ibm.pc] How do you flush the PC's keyboard?

tr@thumper.UUCP (05/29/87)

[If this line had not been eaten,

According to _Advanced MS-DOS_ by Ray Duncan, DOS function C (hex)
resets the input buffer and then gets a character for input using one
of five DOS functions.  They are 1, 6, 7, 8, and A (hex).  I want to
flush the keyboard without getting input.  The reason for this is that
the next thing my program does is call up a canned function that uses
keyboard input.

I am using Lattice C.  Is there a standard way of doing this with C or
Assembler?  I would not expect

	fflush(stdin);

to work because fflush is said to be for output buffers.

Thank you for your attention.  You people are great.

-- 
Tom Reingold
INTERNET:       tr@bellcore.bellcore.com
UUCP: 		{seismo,ihnp4,ucbvax,decvax}!bellcore!tr
		{ulysses,allegra,clyde,princeton}!bellcore!tr

darrylo@hpsrlc.HP.COM (Darryl Okahata) (06/02/87)

In comp.sys.ibm.pc, tr@thumper.UUCP writes:
>
>According to _Advanced MS-DOS_ by Ray Duncan, DOS function C (hex)
>resets the input buffer and then gets a character for input using one
>of five DOS functions.  They are 1, 6, 7, 8, and A (hex).  I want to
>flush the keyboard without getting input.  The reason for this is that
>the next thing my program does is call up a canned function that uses
>keyboard input.
>
>I am using Lattice C.  Is there a standard way of doing this with C or
>Assembler?  I would not expect
>
>	fflush(stdin);
>
>to work because fflush is said to be for output buffers.
>
>Thank you for your attention.  You people are great.
>
>-- 
>Tom Reingold
>INTERNET:       tr@bellcore.bellcore.com
>UUCP: 		{seismo,ihnp4,ucbvax,decvax}!bellcore!tr
>		{ulysses,allegra,clyde,princeton}!bellcore!tr
>----------

     With assembler, you use two DOS functions (I don't remember the
function numbers): one which checks to see if a keyboard character is
available, and another to read a keyboard character.  From here, it's
simple (given in C pseudocode):

	while (character_available())
		read_character();	/* throw read characters into the
					   bit bucket */

I don't know of any *standard* way to do this.

     -- Darryl Okahata
        ucbvax!ucbcad!ames!hplabs!hpcea!hpsrla!darrylo		<== best path
	hplabs!hpcea!hpsrla!darrylo				<== alternative
	CompuServe: 75206,3074

Disclaimer: the above is the author's personal opinion and is not the
opinion or policy of his employer or of the little green men that
have been following him all day.

mjg@ecsvax.UUCP (06/02/87)

In article <747@thumper.UUCP>, tr@thumper.UUCP writes:
> According to _Advanced MS-DOS_ by Ray Duncan, DOS function C (hex)
> resets the input buffer and then gets a character for input using one
> of five DOS functions.  They are 1, 6, 7, 8, and A (hex).  I want to
> flush the keyboard without getting input.  The reason for this is that
> the next thing my program does is call up a canned function that uses
> keyboard input.
> 

Here is one way to clear the typahead buffer that I
am using successfully in one of my programs.
Since the buffer has been flushed there will not be anything
there but I call function 06 anyway as you MUST specify one
of the functions 1,6,7,8 or A anyway. By using 6 it just checks
the buffer which is empty and returns immediately without waiting.
If by some remote chance a key has been pressed in the split second
etween flushing the buffer and returning then it will be discarded.
by the 06 calls.

----------------------------------------------------
; Clear typeahead buffer routine
; uses flush buffer call, which must specify
; a keyboard command to follow. This method uses
; the int 21h call # 06 which reads the keyboard
; but does not wait for a character

ctahed	proc	near 
	mov     ah,0ch  ;flush keyboard buffer
	mov	al,06   ;function forces a read
	mov     dl,0ffh ;int 21, 06 afterwards.
	int     21h
	jz      nochar  ;
	or      al,al   ;if extended char was typed
	jz      nochar  ;get rid if it by another
	mov     al,6    ;read
	mov	dl,0ffh
	int	21h
nochar: ret
ctahed	endp
----------------------------------------------------

     ......decvax!mcnc!ecsvax!mjg

bmarsh@cod.UUCP (06/02/87)

In article <747@thumper.UUCP>, tr@thumper.UUCP writes:
 
> According to _Advanced MS-DOS_ by Ray Duncan, DOS function C (hex)
> resets the input buffer and then gets a character for input using one
> of five DOS functions.  They are 1, 6, 7, 8, and A (hex).

They always forget to add the fact that you can use this function with a
'function' of zero.  This will flush the keyboard input buffer, then return
without asking for a character, exactly as you wanted.

> I am using Lattice C.  Is there a standard way of doing this with C or
> Assembler?

	bdos(0x0c, 0x00, 0x00);

	or

	mov	ax,0c00h
	int	21h

> Thank you for your attention.  You people are great.

	Thanks.  (blush ;-)

> -- 
> Tom Reingold
> INTERNET:       tr@bellcore.bellcore.com
> UUCP: 		{seismo,ihnp4,ucbvax,decvax}!bellcore!tr
> 		{ulysses,allegra,clyde,princeton}!bellcore!tr

---------
Bill Marsh, Naval Ocean Systems Center, San Diego, CA
{arpa,mil}net: bmarsh@cod.nosc.mil
uucp: {ihnp4,akgua,decvax,dcdwest,ucbvax}!sdcsvax!nosc!bmarsh

"If everything seems to be coming your way, you're probably in the wrong lane."

markg@amd.UUCP (06/03/87)

In article <708@cod.UUCP> bmarsh@cod.UUCP (William C. Marsh) writes:
>They always forget to add the fact that you can use this function with a
>'function' of zero.  This will flush the keyboard input buffer, then return
>without asking for a character, exactly as you wanted.
>

This is a first in a long time.  I finally got some useful information
from this newsgroup.  I didn't learn much about how to UNFORMATT a floppy or
why I would like to make PC|MSDOS crash via =" ".  Thank GOD someone out there
has brains!! 

-- 
 Mark Gorlinsky - AMD Processor Products Division/APPS SQA
 UUCP: {decwrl,ihnp4,allegra}!amd!markg
 AT&T: (408) 982-7811
 DISCLAIMER: My opinions are mine, not my employers. 

cjdb@sphinx.UUCP (06/03/87)

In article <4063@amd.UUCP> markg@amd.UUCP (Mark Gorlinsky) writes:
>[...]
>This is a first in a long time.  I finally got some useful information
>from this newsgroup.  I didn't learn much about how to UNFORMATT a floppy or
>why I would like to make PC|MSDOS crash via =" ".  Thank GOD someone out there
>has brains!! 
>

We all have brains, and that's >" ", by the way. 

Here's how I flush the keyboard when I'm not busy freezing up the
machine ( :-) ):

	mov	ah,0		; BIOS function to read char from kbd
	int	16h		; call BIOS

AH contains the scan code, AL the character. Don't process = throw away.

I'm not saying this is better than the other methods posted (after
all, one is counselled to use DOS wherever one can), but simply
different.




-- 
Bitnet:	  	 lib.cb@uchicago.bitnet
Internet:      lib.cb@chip.uchicago.edu
uucp:	  ..!ihnp4!gargoyle!sphinx!cjdb

markg@amd.UUCP (06/03/87)

In article <3320019@hpsrlc.HP.COM> darrylo@hpsrlc.HP.COM (Darryl Okahata) writes:
>In comp.sys.ibm.pc, tr@thumper.UUCP writes:
>>
>     With assembler, you use two DOS functions (I don't remember the
>function numbers): one which checks to see if a keyboard character is
>available, and another to read a keyboard character.  From here, it's
>simple (given in C pseudocode):
>
>	while (character_available())
>		read_character();	/* throw read characters into the
>					   bit bucket */
>

The shortest way (from the view point of your own program) is to use
DOS Function 0Ch with AL=0.  This function will clear the entire keyboard
buffer and only requires two lines of assembly or one line of C code.

examples:
	(assembly)	mov ax,0C00h
			int 21h

	(C)		bdos(0x0C, 0, 0);   (Turbo C, MSC,or Lattice) 

-- 
 Mark Gorlinsky - AMD Processor Products Division/APPS SQA
 UUCP: {decwrl,ihnp4,allegra}!amd!markg
 AT&T: (408) 982-7811
 DISCLAIMER: My opinions are mine, not my employers. 

platt@emory.UUCP (06/06/87)

In article <747@thumper.UUCP> tr@thumper.UUCP writes:
> ...I want to flush the keyboard without getting input.
>flush the keyboard without getting input.  
>I am using Lattice C.  Is there a standard way of doing this with C or
>Assembler?  I would not expect
>	fflush(stdin);
>to work because fflush is said to be for output buffers.

Actually, I've found fflush(stdin) works quite well here.  It's true that
fflush is usually used for output, and isn't needed for UNIX C, but I've
found that functions such as getchar() have a problem with the carriage
return without flushing after getting only one character.  Without the 
fflush(stdin) the next getchar() returns a '\n'.  But then, I'm using
MS C ver 4.0, but the routines are probably similarly quirky with Lattice C.
Anyway, I've seen it work, and I've used it for the above problem.  Hope
it's a help, and I hope it works with lattice C.

Dan