[comp.lang.pascal] Clearing the keyboard buffer

karlth@rhi.hi.is (Karl Thoroddsen) (10/02/89)

While writing in TURBO 4.0 I've constantly encountered the problem
of not being able to flush(clear) the keyboard buffer.

Is there anyone out there who can help me?

Thanks in advance

Karl Thoroddsen

bkottman@wright.EDU (Brett Kottmann) (10/03/89)

in article <1169@krafla.rhi.hi.is>, karlth@rhi.hi.is (Karl Thoroddsen) says:
> 
> While writing in TURBO 4.0 I've constantly encountered the problem
> of not being able to flush(clear) the keyboard buffer.
> 
> Is there anyone out there who can help me?
> 
> Thanks in advance
> 
> Karl Thoroddsen


	readln;

	or

	readln(garbage_variable);

should do it.  Although I'm shure there is a more elegant solution.


							Brett Kottmann
							Wright State U.

=======================================================================
You may already be the winner of 500 million dollars
<rip>
but then again, so could any of the other 3 billion people we mailed
this too! :)
=======================================================================

ts@chyde.uwasa.fi (Timo Salmi LASK) (10/03/89)

In article <1169@krafla.rhi.hi.is> karlth@rhi.hi.is (Karl Thoroddsen) writes:
>While writing in TURBO 4.0 I've constantly encountered the problem
>of not being able to flush(clear) the keyboard buffer.

There is such a routine called CLB in my Pascal units collection
/pc/ts/tspas14.arc.  If you are interested, you can obtain it by
anonymous ftp from our site. 

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun

baldwin@usna.MIL (J.D. Baldwin) (10/03/89)

In article <708@thor.wright.EDU> bkottman@wright.EDU (Brett Kottmann) writes:
>in article <1169@krafla.rhi.hi.is>, karlth@rhi.hi.is (Karl Thoroddsen) says:
>>[asks how to clear the keyboard input buffer in Turbo 4.0]
>
>	readln;
>	or
>	readln(garbage_variable);
>
>should do it.  Although I'm shure there is a more elegant solution.

Well, I'm a Turbo 5.0 man myself (never used 4.0), but I can't imagine that
this would work.  If there are characters in the input buffer *after* the
last <CR>, or if there has been no <CR>.  Also, Brett's solution has the
(possibly extremely serious) defect that it will pause and ask for an
unintended input if there is nothing in the input buffer.

My solution:

        while KeyPressed do ReadKey;
--
From the catapult of:               |+| "If anyone disagrees with anything I
   _, J. D. Baldwin, Comp Sci Dept  |+| say, I am quite prepared not only to
 __||____..}->     US Naval Academy |+| retract it, but also to deny under
 \      / baldwin@cad.usna.navy.mil |+| oath that I ever said it." --T. Lehrer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

filbo@gorn.santa-cruz.ca.us (Bela Lubkin) (10/03/89)

In article <1169@krafla.rhi.hi.is>, Karl Thoroddsen writes:
> While writing in TURBO 4.0 I've constantly encountered the problem
> of not being able to flush(clear) the keyboard buffer.

Var Crud: Char;
..
  While KeyPressed Do Crud:=ReadKey;  { Flush keyboard buffer }

Bela Lubkin     * *   filbo@gorn.santa-cruz.ca.us   CIS: 73047,1112
     @        * *     ...ucbvax!ucscc!gorn!filbo    ^^^  REALLY slow [months]
R Pentomino     *     Filbo @ Pyrzqxgl (408) 476-4633 & XBBS (408) 476-4945

jeff@carroll1.UUCP (Jeff Bartig) (10/03/89)

In article <1169@krafla.rhi.hi.is> karlth@rhi.hi.is (Karl Thoroddsen) writes:
>While writing in TURBO 4.0 I've constantly encountered the problem
>of not being able to flush(clear) the keyboard buffer.

try using:

WHILE keypressed DO
  ch:=readkey;

Where ch is a character variable and there is a "USES crt" at the
beginning of your program.

Jeff

-- 
 Jeff Bartig, Carroll College   |     "The sooner you fall
 jeff@carroll1.cc.edu           |   behind, the more time you
 uunet!marque!carroll1!jeff     |       have to catch up."

d88-eli@nada.kth.se (Erik Liljencrantz) (10/03/89)

In article <1169@krafla.rhi.hi.is> karlth@rhi.hi.is (Karl Thoroddsen) writes:
>While writing in TURBO 4.0 I've constantly encountered the problem
>of not being able to flush(clear) the keyboard buffer.

One simple way using the CRT unit:
  WHILE KeyPressed DO
    Ch:=ReadKey;              { Ch is CHAR }

Do not use ReadLn as this waits for a return to terminate the input.


-- 
Erik Liljencrantz     | "No silly quotes!!"
d88-eli@nada.kth.se   |  Embraquel D. Tuta

TBC101@PSUVM.BITNET (Thomas B. Collins, Jr.) (10/03/89)

In article <48.filbo@gorn.santa-cruz.ca.us>, filbo@gorn.santa-cruz.ca.us (Bela
Lubkin) says:
>
>In article <1169@krafla.rhi.hi.is>, Karl Thoroddsen writes:
>> While writing in TURBO 4.0 I've constantly encountered the problem
>> of not being able to flush(clear) the keyboard buffer.
>
>Var Crud: Char;
>..
>  While KeyPressed Do Crud:=ReadKey;  { Flush keyboard buffer }
>
   If Crud = #0 then Crud := ReadKey;  { Use in case of extended key code}

If the last character in the buffer was "extended" (Alt-something,
etc.), Keypressed will be false, but there will still be a value in
ReadKey to be read.  If you forget to read this, it can cause trouble
later on...

-------
Tom "Shark" Collins       Since ICS is comprised of 2 people, my views
tbc101@psuvm.psu.edu      are the opinion of at least 50% of the company.

ts@chyde.uwasa.fi (Timo Salmi LASK) (10/03/89)

In article <708@thor.wright.EDU> bkottman@wright.EDU (Brett Kottmann) writes:
>in article <1169@krafla.rhi.hi.is>, karlth@rhi.hi.is (Karl Thoroddsen) says:
>> While writing in TURBO 4.0 I've constantly encountered the problem
>> of not being able to flush(clear) the keyboard buffer.
>
>	readln;
>	or
>	readln(garbage_variable);
>should do it.  Although I'm shure there is a more elegant solution.

I am afraid that this is not a working solution at all for at least
two reasons.  The flushing in it only takes place when the enter key
is pressed, and it does not work for the special keys such as F1 or
esc.  The other solution that has been suggested (while KeyPressed
do ch := ReadKey;) is better, but it is also problematic if the
ensuing input involves the special keys with ReadKeys.  And if the
input and output get out of phase, it is not guaranteed to have a
desired affect.  What one has to do is to resort to system services. 
Interrupt 21Hex service 0CHex can be used for the flushing.  Norton
& Wilton is a good source for further details.  (If you disagree,
ok, Brett, but would you please not make it a personal attact.)

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun

ckurak@unf7.UUCP (Charles Kurak) (10/03/89)

In article <1169@krafla.rhi.hi.is> karlth@rhi.hi.is (Karl Thoroddsen) writes:
>While writing in TURBO 4.0 I've constantly encountered the problem
>of not being able to flush(clear) the keyboard buffer.
>
>Is there anyone out there who can help me?
>
>Thanks in advance
>
>Karl Thoroddsen
I've used the following:

procedure ClearTypeAhead;
var ch : char;
begin
  repeat
  if keypressed then
    ch := readkey;
  until not keypressed;
end;

Also, remember "Uses CRT" at the beginning of your code so keypressed will be
defined.

baldwin@usna.MIL (LT Justin D. Baldwin <baldwin@usna>) (10/03/89)

I boneheadedly wrote (to clear the keyboard buffer):
>        while KeyPressed do ReadKey;

when of course I meant 

          while KeyPressed do SomeNullCharVar := ReadKey;

I know this has been said, but "C"ancel doesn't work around here, and
I hate the idea of bogus info like that floating around uncorrected.

My analysis of the flaws in the original solution stands.
--
From the catapult of:               |+| "If anyone disagrees with anything I
   _, J. D. Baldwin, Comp Sci Dept  |+| say, I am quite prepared not only to
 __||____..}->     US Naval Academy |+| retract it, but also to deny under
 \      / baldwin@cad.usna.navy.mil |+| oath that I ever said it." --T. Lehrer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

brianr@phred.UUCP (Brian Reese) (10/04/89)

In article <246@usna.MIL> baldwin@cad.usna.mil (J.D. Baldwin) writes:
>In article <708@thor.wright.EDU> bkottman@wright.EDU (Brett Kottmann) writes:
>>in article <1169@krafla.rhi.hi.is>, karlth@rhi.hi.is (Karl Thoroddsen) says:
>>>[asks how to clear the keyboard input buffer in Turbo 4.0]
>>
>>	readln;
>>	or
>>	readln(garbage_variable);
>>
>>should do it.  Although I'm shure there is a more elegant solution.
>
>Well, I'm a Turbo 5.0 man myself (never used 4.0), but I can't imagine that
>this would work.  If there are characters in the input buffer *after* the
>last <CR>, or if there has been no <CR>.  Also, Brett's solution has the
>(possibly extremely serious) defect that it will pause and ask for an
>unintended input if there is nothing in the input buffer.
>My solution:
>
>        while KeyPressed do ReadKey;
>
>   _, J. D. Baldwin, Comp Sci Dept  |+| say, I am quite prepared not only to

Hello all,

I agree with J.D. in that Brett's solution could pause for unintended input
should the buffer be empty.  In fact, I e-mailed a solution similar to J.D.'s
to the original poster.  However, ReadKey is a function which returns a value
and must be assigned to a variable, i.e.

	var Ch : char;

	while KeyPressed do Ch := ReadKey;

I've used this many times, but often wondered if there is an interrupt to do
the same thing.  Does anyone out there know?


Brian
"I've learned C.  It's an extremly powerful language that allows a programmer
to do incredibly stupid things!  I still love my Pascal compiler!!!!"



-- 
Brian Reese                           uw-beaver!pilchuck!seahcx!phred!brianr
Physio Control Corp., Redmond, Wa.                         brianr@phred.UUCP
"Do not write on this line.  This line has been left blank intentionally."

d88-eli@nada.kth.se (Erik Liljencrantz) (10/04/89)

In article <2774@phred.UUCP> brianr@phred.UUCP (Brian Reese) writes:
[...]
>	var Ch : char;
>
>	while KeyPressed do Ch := ReadKey;
>
>I've used this many times, but often wondered if there is an interrupt to do
>the same thing.  Does anyone out there know?

Yes: INT 16h, function 0 (in AH) reads a scancode into AX. Normal characters
are returned in AL (easy to translate to CHAR), but if the key is an extended
key (like F1, PgUp...) AL is zero, and AH contains the scancode. (The ReadKey
function has to be called twice if the first call returned a null (i.e. #0).)

BTW: Check the interruptlist (INTER489) for this kind of information. INT 16
     covers several other keyboard functions.
-- 
Erik Liljencrantz     | "No silly quotes!!"
d88-eli@nada.kth.se   |  Embraquel D. Tuta

john@uunet.uu.net (John Baima) (10/04/89)

RE: Clearing the keyboard buffer.

Using Readln is a poor solution, I think because the program will stop
if nothing has been typed. The solution I usually use is something
like:

While KeyPressed do ch := ReadKey;

John Baima
texbell!utafll!john

filbo@gorn.santa-cruz.ca.us (Bela Lubkin) (10/04/89)

In article <89276.021637TBC101@PSUVM.BITNET> Thomas B. Collins, Jr. writes:
>In article <48.filbo@gorn.santa-cruz.ca.us>, Bela Lubkin says:
>>  While KeyPressed Do Crud:=ReadKey;  { Flush keyboard buffer }
>   If Crud = #0 then Crud := ReadKey;  { Use in case of extended key code}

>If the last character in the buffer was "extended" (Alt-something,
>etc.), Keypressed will be false, but there will still be a value in
>ReadKey to be read.  If you forget to read this, it can cause trouble
>later on...

This isn't true in any version of Turbo Pascal that I've ever used...  The
keyboard handling code has always worked as follows: call the BIOS to check
for a key.  If it's not "extended", return the value.  If it is "extended",
save the value in a temporary variable and return the prefix value (ESC before
Turbo 4.0, NUL after).  KeyPressed has always been cognizant of the saved
extended value.

In fact, in Turbo 3.0 I used code such as:

  Read(Kbd,Ch);
  Extended:=(Ch=ESC) And KeyPressed;
  If Extended Then Read(Kbd,Ch);

to read keystrokes, including extended characters.  In 4/5/5.5:

  Ch:=ReadKey;
  Extended:=(Ch=NUL) And KeyPressed;
  If Extended Then Ch:=ReadKey;

The check of KeyPressed isn't strictly necessary in 4/5/5.5 since it is very
difficult to get the IBM BIOS to generate a NUL character from the keyboard,
but is probably good practice anyway.  On the other hand, it was necessary in
Turbo 3.0 to prevent the program from blocking on the ESC key.  The 3.0 code
was susceptible to typed-ahead ESC "char" being interpreted as an extended
sequence; in programs where that really mattered, I found the appropriate
character in the runtime library and patched it to a NUL...

Bela Lubkin     * *   filbo@gorn.santa-cruz.ca.us   CIS: 73047,1112
     @        * *     ...ucbvax!ucscc!gorn!filbo    ^^^  REALLY slow [months]
R Pentomino     *     Filbo @ Pyrzqxgl (408) 476-4633 & XBBS (408) 476-4945

unkydave@shumv1.uucp (David Bank) (10/05/89)

In article <1169@krafla.rhi.hi.is> karlth@rhi.hi.is (Karl Thoroddsen) writes:
>While writing in TURBO 4.0 I've constantly encountered the problem
>of not being able to flush(clear) the keyboard buffer.
>
>Is there anyone out there who can help me?
>
>Thanks in advance
>
>Karl Thoroddsen


     I have a routine in TP 3.0 that does that. It goes....

           WHILE KeyPressed DO
                 read(kbd, dummy);

      Where "dummy" is a character and "KeyPressed" is the TP 3.0
function that checks to see if the keyboard buffer holds any characters.

       Even tho I have TP 4.0, I haven't bothered to convert this yet.
I think "KeyPressed" was renamed in TP 4.0 and you use "readkey" instead
of "read(kbd)". Don't have all the details, but that should do the
trick.

      Hope it helps.....

(Another wonderful device from Unky Dave's bag of software tricks!  :-)

Dave

leif@ambush.dk (Leif Andrew Rump) (10/05/89)

This does the job!

  while keypressed do
    while readkey = #0 do
      (* nothing *);

And I don't need a dummy variable!

  Leif Andrew Rump, AmbraSoft A/S, Roejelskaer 15, DK-2840 Holte, Denmark
 UUCP: leif@ambra.dk, phone: +45 42424 111, touch phone: +45 42422 817+313

   > > > Why are tall Irish girls with red hair so wonderful ? ? ? < < <