Keyboard Unit
Download keyboard.zip (2466 bytes)
{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=-=- -=-=-=-=-=-=
-=-=-= Keyboard Interface Unit =-=-=-
=-=- -=-=
-=-=-= Coded by: Chris Lattner -=-=-=
=-=-=-=-=-=- 1995 -=-=-=-=-=-=
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}
UNIT Keyboard;
INTERFACE
USES DOS;
CONST { Commonly used scan codes }
ScEsc = 1;
ScUp = 72;
ScLeft = 75;
ScRight = 77;
ScDown = 80;
VAR
Keys : ARRAY[0..255] OF BOOLEAN;
(* Example program - Here is a program that demonstrates the commonly used
functions available in this unit:
VAR
X, Y : INTEGER;
BEGIN
InstallKBHandler(False);
WHILE NOT Keys[ScESC] DO
BEGIN
IF Keys[ScUp ] THEN DEC(Y);
IF Keys[ScDown ] THEN INC(Y);
IF Keys[ScLeft ] THEN DEC(X);
IF Keys[ScRight] THEN INC(X);
DrawFrame(X, Y);
END;
RemoveKBHandler;
END. *)
PROCEDURE InstallKBHandler(CallBios : BOOLEAN);
(* InstallKBHandler - Installs a low level keyboard handler that can handle
multiple keys down. If the "CallBios" parameter is TRUE then the Bios and
DOS functions can be used to access the keyboard. READLN, READKEY and the
like will work. This is slower, and you will get an annoying beep if the
BIOS's buffer gets full. I recommend only using this mode when debuging.
The other mode of operation is activated when "CallBios" is FALSE. The
BIOS's handler doesn't get called. It is a faster and more versitile way
of accessing the keyboard. Whenever a key is pressed, the replacement
handler calculates the scancode and sets in the element in the "Keys" with
the corresponding number. When it is released, it clears the element. The
main problem with this mode is when debuging. CTRL-Break is ignored, so
debugging becomes more of a problem. Because of this, I often use this
statement when I first initialize the keyboard:
InstallKBHandler({$IFDEF Debug THEN} TRUE {$ELSE} FALSE {$ENDIF});
In the main loop of your program, call "ClearBiosKbBuffer" and all of the
annoying beeps will be stopped. *)
PROCEDURE RemoveKBHandler;
(* RemoveKBHandler - Removes the low level keyboard handler. MUST be
called if the handler is installed. (The keyboard would be in-operable
after the main program terminates if the custom handler is not removed) *)
PROCEDURE ClearBiosKbBuffer;
(* ClearBiosKbBuffer - Has the effect of the following statement:
WHILE NOT Keypressed DO ReadKey;
But of course, it is much faster and simpler. *)
Created by Chris Lattner 1995