{$D-} unit Keys; interface procedure InitKeys; procedure CloseKeys; function Pressed(Key: byte): boolean; const HaltProc: procedure = nil; implementation var Old09: procedure; KeyState: array [0 .. 31] of byte; Interruptions: array [byte] of procedure absolute $0000: $0000; WasK: byte; procedure New09; interrupt; var Key: byte; begin Key := Port[$60]; asm pushf end; Old09; if Key = $E0 then WasK := WasK or 1 else if (Key = $2A) and Odd(WasK) then WasK := 2 else begin if Key < $80 then begin if Odd(WasK) then KeyState[Key shr 3 + 16] := KeyState[Key shr 3 + 16] or (1 shl (Key and 7)) else KeyState[Key shr 3] := KeyState[Key shr 3] or (1 shl (Key and 7)); end else begin if Odd(WasK) then KeyState[Key shr 3] := KeyState[Key shr 3] and not (1 shl (Key and 7)) else KeyState[Key shr 3 - 16] := KeyState[Key shr 3 - 16] and not (1 shl (Key and 7)); end; WasK := 0; if Odd(KeyState[3] shr 5) and Odd(KeyState[5] shr 6) then begin WriteLn('CTRL+C pressed. Program terminated.'); CloseKeys; if @HaltProc <> nil then HaltProc; Halt; end; end; end; procedure InitKeys; begin FillChar(KeyState, SizeOf(KeyState), 0); Old09 := Interruptions[$09]; @Interruptions[$09] := @New09; end; procedure CloseKeys; begin Interruptions[$09] := Old09; end; function Pressed(Key: byte): boolean; begin Pressed := odd(KeyState[Key shr 3] shr (Key and 7)); end; end.