summaryrefslogtreecommitdiff
path: root/packages/rtl-console/tests/kbdtest.pp
blob: c508f912118e5d1034a1823d04f28d85aafdade4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
program KbdTest;

{$MODE objfpc}{$H+}

uses
  Video, Keyboard, Mouse, kbdutil, vidutil;

const
  LastPressedAttr = $E0;
  PreviouslyPressedAttr = $0F;
  NotPressedAttr = $6F;
  NotAvailableAttr = $08;
var
  kbd: TKeyboard;
  KbdEventMap: array of array [0..1] of TKeyEvent;
  KeyHasBeenPressed: array of Boolean;
  DumpF: TextFile;
  I: Integer;
  a1, a2: Int64;
  K, TK: TKeyEvent;
  M: TMouseEvent;
  FoundKey: Boolean;
begin
  if ParamCount <> 2 then
  begin
    Writeln('Usage: ', ParamStr(0), ' <kbd_file> <dump_file>');
    Halt(1);
  end;


  InitVideo;
  InitKeyboard;
  InitMouse;

  kbd := ReadKeyboardFromFile(ParamStr(1));
  SetLength(KbdEventMap, Length(kbd.Keys));
  SetLength(KeyHasBeenPressed, Length(kbd.Keys));
  AssignFile(DumpF, ParamStr(2));
  Reset(DumpF);
  for I := Low(kbd.Keys) to High(kbd.Keys) do
  begin
    KeyHasBeenPressed[I] := False;
    Readln(DumpF, a1, a2);
    if (a1 = -1) or (a2 = -1) then
    begin
      KbdEventMap[I][0] := 0;
      KbdEventMap[I][1] := 0;
      DrawKey(kbd.Keys[I], NotAvailableAttr);
    end
    else
    begin
      KbdEventMap[I][0] := a1;
      KbdEventMap[I][1] := a2;
      DrawKey(kbd.Keys[I], NotPressedAttr);
    end;
  end;
  CloseFile(DumpF);

  TextOut(1, 20, 'Press each of the highlighted keys.', $07);
  TextOut(1, 21, 'Click the right mouse button to exit.', $07);
  UpdateScreen(False);

  repeat
    repeat
      K := PollKeyEvent;
      if PollMouseEvent(M) then
        GetMouseEvent(M);
    until (K <> 0) or ((GetMouseButtons and MouseRightButton) <> 0);
    if K <> 0 then
    begin
      K := GetKeyEvent;
      TK := TranslateKeyEvent(K);

      FoundKey := False;
      for I := Low(kbd.Keys) to High(kbd.Keys) do
        if (KbdEventMap[I][0] = K) and (KbdEventMap[I][1] = TK) then
        begin
          FoundKey := True;
          KeyHasBeenPressed[I] := True;
          DrawKey(kbd.Keys[I], LastPressedAttr);
        end
        else if KeyHasBeenPressed[I] then
          DrawKey(kbd.Keys[I], PreviouslyPressedAttr)
        else if KbdEventMap[I][0] <> 0 then
          DrawKey(kbd.Keys[I], NotPressedAttr)
        else
          DrawKey(kbd.Keys[I], NotAvailableAttr);

      if not FoundKey then
      begin
        TextOut(1, 18, 'Unknown key code.', $04);
      end;
      UpdateScreen(False);
    end;
  until (GetMouseButtons and MouseRightButton) <> 0;

  DoneMouse;
  DoneKeyboard;
  DoneVideo;
end.