[comp.lang.pascal] Reading the keyboard

wsinpvb@eutrc3.urc.tue.nl (p.v.bemmelen) (08/13/89)

Hello
I am writing a program (in fact a game) that needs to read the keyboard.
Of course the standard method is not good enough because it has a delay
before the key starts repeating, and if you press multiple keys the result
is incorrect.
What i need is the following:
the user should be able to press multiple keys, and the program should
detect all of them, not just the last one pressed.
The program should read which keys are pressed at that moment, not which
have been pressed lately

I believe this requires programming of the keyboard controller, or reading portsdirectly. The language is Turbo Pascal 4,5 of 5.5, but examples in C or assembler are also welcome. Using the intr() function is no problem.

Greetings
wsinpvb@eutrc3.UUCP

chad@csd4.csd.uwm.edu (D. Chadwick Gibbons) (08/23/89)

In article <835@eutrc3.urc.tue.nl> p.v.bemmelen writes:
|the user should be able to press multiple keys, and the program should
|detect all of them, not just the last one pressed.

	To have this work properly, the main loop of your program needs to be
a keyboard scanning routine.  This can be done very easily in Turbo Pascal,
accomplished with a routine such as:
	
	FUNCTION InKey : CHAR;
	BEGIN
	  Port[$60] := $00; { set "no delay" repeat }
	  WHILE NOT (KeyPressed) DO
	    ;
	  InKey := ReadKey
	END;

This is all you need.  Repeats will be handled as expected: upon they next
call, they are still in the buffer, and will be able to read immediately read
from this function.

	A few things to note: setting the repeat delay above may not work on 
XT class machines.  I don't have access to an XT, but I have seen this used in
one of Borland's OOP demonstration programs included with version 5.5 of TP.
You'll have to experiment to determine if this is a portable usage.  It makes
no real difference to the program, but a faster repeat is indispensable, in my
opinion.  Note too that ReadKey returns zero if an extended key has been
pressed.  If you're using InKey as an expression in the case, check for zero
and read the buffer again, i.e.:

	ch := InKey;
	CASE ch OF
	  0: ch := ReadKey; { get extended key }
	  ...
	  ...
	END;

	Keyboard reading is easily accomplished using Turbo's own built in
function and really does not require any external interference unless you are
preforming actions based upon control keys being pressed (i.e. if ALT is
touched, the status line may display another set of functions, etc, etc.)
--
D. Chadwick Gibbons (chad@csd4.csd.uwm.edu)

TBC101@PSUVM.BITNET (Thomas B. Collins, Jr.) (09/26/89)

I'm working on a program with Turbo Pascal 5.5, and I've noticed that
the F11 and F12 keys don't trigger the 'ReadKey' command.  Am I doing
something wrong, or do I need to do something else in order to read
these keys?  It should send a null to the first ReadKey, and then a 133
(F11) or 134 (F12) to the second ReadKey.  As it stands now, it doesn't
even send a null...

-------
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) (09/27/89)

In article <89268.185834TBC101@PSUVM.BITNET> TBC101@PSUVM.BITNET (Thomas B. Collins, Jr.) writes:
>I'm working on a program with Turbo Pascal 5.5, and I've noticed that
>the F11 and F12 keys don't trigger the 'ReadKey' command.  Am I doing
>something wrong, or do I need to do something else in order to read
>these keys?  It should send a null to the first ReadKey, and then a 133

The F11 and F12 keys are part of the extended keyboard, and to
access them you have to use ROM BIOS keyboard services.  The
extended keyboard can be accessed using interrupt 16Hex, service
10Hex.  The main byte is returned in register al, and the auxiliary
byte in the register ah.  If this sounds complicated, see the code
at the end.  For more information see Norton & Wilton, the New Peter
Norton Programmer's Guide to the IBM PC & PS/2, or INTER489.ARC,
available by anynymous ftp from all well stocked sites (and also
ours :-)
...................................................................
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


(* Keyscan by Timo Salmi *)
uses Dos;
var regs : registers;
begin
  FillChar (regs, SizeOf(regs), 0);
  regs.ax := $1000;
  Intr ($16, regs);
  writeln (regs.al, ' ', regs.ah);
end.