[comp.lang.pascal] Preset KeyPressed Function?

ndeng@EULER.BERKELEY.EDU (11/26/90)

Hi Netters,

I have a TP 5.5 problem which bothered me for several days but I cannot
find an answer. So if any of your gurus can give me a clue:

The problem is: I am using ReadKey function for keyboard input. By
default, the KeyPressed function should always be false so the keyboard
will wait until the next keystroke. However, While I use ReadKey function
for keyboard input, KeyPressed function occasional becomes true. (It only
happens in the interactive mode, when I recompile the program, the problem
disappears).

I am not sure if this is a bug or a feature of TP? IS there any way that I
can preset the value of the KeyPressed function?

Please reply to this account since I don't read the net regularly. If this
is of general interest, I'll post a summary.


Mike
ndeng@euler.berkeley.edu

mcastle@mcs213a.cs.umr.edu (Mike Castle) (11/26/90)

I had a similiar problem using ReadKey.  Turned out it was all poor programming.

I was using  a    repeat until keypressed   routine at the ends of some 
procedures so I could check my output (was directing it to screen during
testing, went to printer later, so I had to put in a hold function).

Unfortunately, that doesn't clear the keyboard buffer.  Any subsequent
keypressed call returned true.  Also, readkey would return that keystroke
immediately.

I've since gone to using  ch:=readkey  when ch is type ch and not used for
anything else.  I don't use it all that often, so I've never written an
official  hold procedure (though I probably should).  

Anyway, if you have a keypressed routine earlier, but not a corresponding
readkey for it, that might be your problem.  Be sure to clear the keyboard
buffer before issuing the next keypressed or whatever.  hmm... what about:

Procedure ClearKbd;

Type
  Ch : Char;

Begin
  While KeyPressed do Ch:=ReadKey;
End;

That looks like it should do it.....
Hope it helps.
-- 
Mike Castle (Nexus) S087891@UMRVMA.UMR.EDU (preferred)       | ERROR:  Invalid
                mcastle@mcs213k.cs.umr.edu (unix mail-YEACH!)| command 'HELP'
Life is like a clock:  You can work constantly, and be right | try 'HELP'
all the time, or not work at all, and be right twice a day.  |

zhou@brazil.psych.purdue.edu (Albert Zhou) (11/26/90)

	The mechanism of "function keypressed" is to check the keyboard
buffer to see if it is empty. So occationally there might be something
still left in the buffer and it will return True.

dave@tygra.ddmi.com (David Conrad) (11/26/90)

In article <1728@umriscc.isc.umr.edu> mcastle@mcs213a.cs.umr.edu (Mike Castle) writes:
>
>I was using  a    repeat until keypressed   routine at the ends of some 
>[...]
>Unfortunately, that doesn't clear the keyboard buffer.  Any subsequent
>keypressed call returned true.  Also, readkey would return that keystroke
>immediately.
>
>I've since gone to using  ch:=readkey  when ch is type ch and not used for
>anything else.
>

It can also be done without wasting an otherwise unused variable:
  ...
  repeat
    ...
  until keypressed;
  if readkey = #0 then if readkey = #0 then;
  ...
This method tosses out the second code returned by those keys which
return two codes (such as F1 and PgDn), an advantage over the above
method.  Hope this helps.
--
  David R. Conrad   | The goal of Artificial Intelligence is to build
dave@tygra.ddmi.com | computers which act like the ones in the movies.
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave@DDMI.COM

ts@uwasa.fi (Timo Salmi) (11/26/90)

In article <1728@umriscc.isc.umr.edu> mcastle@mcs213a.cs.umr.edu (Mike Castle) writes:
... much deleted ...
>
>Procedure ClearKbd;
>
>Type
>  Ch : Char;
>
>Begin
>  While KeyPressed do Ch:=ReadKey;
>End;
>
>That looks like it should do it.....

Allow me to add some material from my /ts/pc/tsfaq14.arc FAQ
collection.  The third method below (which should be the most
robust) can be found preprogrammed in my /pc/ts/tspas22.arc Turbo
Pascal units at uwasa.fi archives. 

16. *****
 Q: How can I clear the keyboard type-ahead buffer.

 A: Three methods are usually suggested for solving this problem.
a) The first is to use something like
     uses Crt;
     var dummy : char;
     while KeyPressed do dummy := ReadKey;
This kludge-type method has the disadvantage of requiring the Crt
unit. Also, in connection with procedures relying on ReadKey for
input, it may cause havoc on the programs logic.
b) The second method accesses directly the circular keyboard buffer
     var head : word absolute $0040:$001A;
         tail : word absolute $0040:$001C;
     procedure FLUSHKB; begin head := tail; end;
c) The third method is to call interrupt 21Hex (the MsDos interrupt)
with the ax register set as $0C00. This method has the advantage of
not being "hard-coded" like the second method, and thus should be
less prone to incompatibility.

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

ace@cc.ic.ac.uk (Andriko del Saludo) (11/27/90)

In article <9011251958.AA08961@euler.Berkeley.EDU> ndeng@EULER.BERKELEY.EDU writes:
>
>Hi Netters,
>
>I have a TP 5.5 problem which bothered me for several days but I cannot
>find an answer. So if any of your gurus can give me a clue:
>
>The problem is: I am using ReadKey function for keyboard input. By
>default, the KeyPressed function should always be false so the keyboard
>will wait until the next keystroke. However, While I use ReadKey function
>for keyboard input, KeyPressed function occasional becomes true. (It only
>happens in the interactive mode, when I recompile the program, the problem
>disappears).
>....Stuff deleted...

I've had the same problem from version 4 to version 5.5. One small
addendum is that the problem appears when I am doing string input and
not anytime else. It feels like a TP bug (I am not sure but I spent a
lot of time trying different coding schemes and none of them ever fixed
it)
ace



-- 
---------------------------------------------------------------------
- Andreas C. Enotiadis (ace@cc.ic.ac.uk, ace@grathun1.earn, etc)    -
- (I'm still thinking about something clever to put here...)        -
---------------------------------------------------------------------