home *** CD-ROM | disk | FTP | other *** search
- (* keywatch.pas 12-13-91 Robert Mashlan, Public Domain
-
- This program monitors the keyboard interrupt, and stores the
- status of each key as to whether it is pressed or released.
-
- This is done by capturing interrupt 9, and watching the make/break
- codes. The status is updated in the keys array, where nonzero means
- that the key is pressed, while 0 means the key is released. The
- key array is uses the scan code for an index instead of the ascii
- character. It is simple enough to find the scan code for a key,
- just run this program and watch the display.
-
- Since this program installs an interrupt handler, it should be
- terminated normally, such the keyboard handler can be removed.
-
- *)
-
- Uses
- Dos, Crt;
-
- var
- keys : array[0..127] of boolean; (* array of key states *)
- OldKBIsr : Procedure; (* address of previous keyboard ISR *)
-
-
- Procedure NewKBIsr; interrupt;
- const
- keyport = $60;
- var
- scancode : byte;
-
- Procedure PushF; inline($9c);
-
- begin
- scancode := port[keyport]; (* read keyboard scan code *)
- if ( scancode <> $e0 ) and ( scancode <> $e1 ) then
- if ( scancode and $80 ) = $80 then (* key released? *)
- keys[scancode and $7f] := false (* it's released *)
- else
- keys[scancode] := true; (* it's pressed *)
- pushf;
- oldkbisr; (* chain to previous keyboard ISR *)
- end;
-
- Function KeysPressed : integer;
- (* returns number of keys being held down *)
- var
- result, i : integer;
- begin
- result := 0;
- for i := 0 to 127 do
- if keys[i] then
- inc(result);
- KeysPressed := result;
- end;
-
- Function HexStr( b : byte ) : string;
- (* form a string of b represented in hex *)
- type
- nibble = 0..$f;
- var
- result : string;
-
- Function NibCh( n : nibble ) : char;
- (* returns hex char given a nibble *)
- begin
- case n of
- 0..9 : NibCh := char( n + ord('0') );
- else NibCh := char( n - $a + ord('a') );
- end;
- end;
-
- begin
- result := '$00';
- result[2] := NibCh( b shr 4 );
- result[3] := NibCh( b and $f );
- HexStr := result;
- end;
-
- Procedure Main;
- var
- LastKeyCount, i : integer;
- begin
- LastKeyCount := 0;
- for i := 0 to 127 do (* intialize array *)
- Keys[i] := false;
- CheckBreak := FALSE; (* ignore ^C and ^Break *)
- GetIntVec($9,@OldKBIsr);
- SetIntVec($9,@NewKBIsr); (* install interrupt handler *)
- repeat
- if KeysPressed <> LastKeyCount then (* change in keystatus? *)
- begin
- clrscr;
- for i := 0 to 127 do
- if keys[i] then
- Writeln('key with scan code ',HexStr(i),' has been pressed');
- lastkeycount := KeysPressed;
- end;
- until KeyPressed and ( ReadKey = #27 ); (* terminate loop when esc pressed *)
- SetIntVec($9,@OldKBIsr); (* remove interrupt handler *)
- end;
-
- begin
- Main;
- end.
-