summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/inc/keyboard.inc
blob: 1c0609f35d0831506e8349d34cdadea2738ae7f2 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

var
  PendingKeyEvent : TKeyEvent;

procedure PutKeyEvent(KeyEvent: TKeyEvent);
begin
  PendingKeyEvent := KeyEvent;
end;

function GetKeyEventFlags(KeyEvent: TKeyEvent): Byte;
begin
  GetKeyEventFlags := (KeyEvent and $FF000000) shr 24;
end;

function GetKeyEventChar(KeyEvent: TKeyEvent): Char;
begin
  if KeyEvent and $03000000 = $00000000 then
    GetKeyEventChar := Chr(KeyEvent and $000000FF)
   else
    GetKeyEventChar := #0;
end;

function GetKeyEventUniCode(KeyEvent: TKeyEvent): Word;
begin
  if KeyEvent and $03000000 = $01000000 then
    GetKeyEventUniCode := KeyEvent and $0000FFFF
   else
    GetKeyEventUniCode := 0;
end;

function GetKeyEventCode(KeyEvent: TKeyEvent): Word;
begin
  GetKeyEventCode := KeyEvent and $0000FFFF
end;

function GetKeyEventShiftState(KeyEvent: TKeyEvent): Byte;
begin
  GetKeyEventShiftState := (KeyEvent and $00FF0000) shr 16;
end;

function IsFunctionKey(KeyEvent: TKeyEvent): Boolean;
begin
  IsFunctionKey := KeyEvent and $03000000 = $02000000;
end;

Var
  KeyBoardInitialized : Boolean;
  CurrentKeyboardDriver : TKeyboardDriver;

procedure InitKeyboard;

begin
  If Not KeyboardInitialized then
    begin
    If Assigned(CurrentKeyBoardDriver.InitDriver) Then
      CurrentKeyBoardDriver.InitDriver();
    KeyboardInitialized:=True;
    end;
end;

procedure DoneKeyboard;

begin
  If KeyboardInitialized then
    begin
    If Assigned(CurrentKeyBoardDriver.DoneDriver) Then
      CurrentKeyBoardDriver.DoneDriver();
    KeyboardInitialized:=False;
    end;
end;


function GetKeyEvent: TKeyEvent;

begin
  if PendingKeyEvent<>0 then
    begin
    GetKeyEvent:=PendingKeyEvent;
    PendingKeyEvent:=0;
    exit;
    end;
  If Assigned(CurrentKeyBoardDriver.GetKeyEvent) Then
    GetKeyEvent:=CurrentKeyBoardDriver.GetKeyEvent()
  else
    GetKeyEvent:=0;
end;

function PollKeyEvent: TKeyEvent;

begin
  if PendingKeyEvent<>0 then
   exit(PendingKeyEvent);
  If Assigned(CurrentKeyBoardDriver.PollKeyEvent) Then
    begin
    PollKeyEvent:=CurrentKeyBoardDriver.PollKeyEvent();
    // PollKeyEvent:=PendingKeyEvent;
    // Must be done inside every keyboard specific
    // PollKeyEvent procedure
    // to avoid problems if that procedure is called directly PM
    end
  else
    PollKeyEvent:=0;
end;

Function SetKeyboardDriver (Const Driver : TKeyboardDriver) : Boolean;

begin
  If Not KeyBoardInitialized then
    CurrentKeyBoardDriver:=Driver;
  SetKeyboardDriver:=Not KeyBoardInitialized;
end;

Procedure GetKeyboardDriver (Var Driver : TKeyboardDriver);

begin
  Driver:=CurrentKeyBoardDriver;
end;

function PollShiftStateEvent: TKeyEvent;

begin
  If Assigned(CurrentKeyBoardDriver.GetShiftState) then
    PollShiftStateEvent:=CurrentKeyBoardDriver.GetShiftState() shl 16
  else
    PollShiftStateEvent:=0;
end;

function DefaultTranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;

begin
  DefaultTranslateKeyEventUniCode:=KeyEvent;
  ErrorCode:=errKbdNotImplemented;
end;


function TranslateKeyEventUniCode(KeyEvent: TKeyEvent): TKeyEvent;

begin
  if Assigned(CurrentKeyBoardDriver.TranslateKeyEventUnicode) then
    TranslateKeyEventUnicode:=CurrentKeyBoardDriver.TranslateKeyEventUnicode(KeyEvent)
  else
    TranslateKeyEventUnicode:=DefaultTranslateKeyEventUnicode(KeyEvent);
end;

type
  TTranslationEntry = packed record
    Min, Max: Byte;
    Offset: Word;
  end;
const
  TranslationTableEntries = 12;
  TranslationTable: array [1..TranslationTableEntries] of TTranslationEntry =
    ((Min: $3B; Max: $44; Offset: kbdF1),   { function keys F1-F10 }
     (Min: $54; Max: $5D; Offset: kbdF1),   { Shift fn keys F1-F10 }
     (Min: $5E; Max: $67; Offset: kbdF1),   { Ctrl fn keys F1-F10 }
     (Min: $68; Max: $71; Offset: kbdF1),   { Alt fn keys F1-F10 }
     (Min: $85; Max: $86; Offset: kbdF11),  { function keys F11-F12 }
     (Min: $87; Max: $88; Offset: kbdF11),  { Shift+function keys F11-F12 }
     (Min: $89; Max: $8A; Offset: kbdF11),  { Ctrl+function keys F11-F12 }
     (Min: $8B; Max: $8C; Offset: kbdF11),  { Alt+function keys F11-F12 }
     (Min: $47; Max: $49; Offset: kbdHome), { Keypad keys kbdHome-kbdPgUp }
     (Min: $4B; Max: $4D; Offset: kbdLeft), { Keypad keys kbdLeft-kbdRight }
     (Min: $4F; Max: $51; Offset: kbdEnd),  { Keypad keys kbdEnd-kbdPgDn }
     (Min: $52; Max: $53; Offset: kbdInsert));


function DefaultTranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;

var
  I: Integer;
  ScanCode: Byte;
begin
  if KeyEvent and $03000000 = $03000000 then
   begin
     if KeyEvent and $000000FF <> 0 then
      begin
        DefaultTranslateKeyEvent := KeyEvent and $00FFFFFF;
        exit;
      end
     else
      begin
        { This is a function key }
        ScanCode := (KeyEvent and $0000FF00) shr 8;
        for I := 1 to TranslationTableEntries do
         begin
           if (TranslationTable[I].Min <= ScanCode) and (ScanCode <= TranslationTable[I].Max) then
            begin
              DefaultTranslateKeyEvent := $02000000 + (KeyEvent and $00FF0000) +
                Byte(ScanCode - TranslationTable[I].Min) + TranslationTable[I].Offset;
              exit;
            end;
         end;
      end;
   end;
  DefaultTranslateKeyEvent := KeyEvent;
end;

function TranslateKeyEvent(KeyEvent: TKeyEvent): TKeyEvent;

begin
  if Assigned(CurrentKeyBoardDriver.TranslateKeyEvent) then
    TranslateKeyEvent:=CurrentKeyBoardDriver.TranslateKeyEvent(KeyEvent)
  else
    TranslateKeyEvent:=DefaultTranslateKeyEvent(KeyEvent);
end;

{ ---------------------------------------------------------------------
    KeyEvent to String representation section.
  ---------------------------------------------------------------------}

Procedure AddToString (Var S : String; Const A : String);

begin
  If Length(S)=0 then
    S:=A
  else
    S:=S+' '+A;
end;

Function IntToStr(Int : Longint) : String;

begin
  Str(Int,IntToStr);
end;

Function ShiftStateToString(KeyEvent : TKeyEvent; UseLeftRight : Boolean) : String;

Var
  S : Integer;
  T : String;

begin
  S:=GetKeyEventShiftState(KeyEvent);
  T:='';
  If (S and kbShift)<>0 then
    begin
    if UseLeftRight then
      case (S and kbShift) of
        kbShift      : AddToString(T,SLeftRight[1]+' '+SAnd+' '+SLeftRight[2]);
        kbLeftShift  : AddToString(T,SLeftRight[1]);
        kbRightShift : AddToString(T,SLeftRight[2]);
      end;
    AddToString(T,SShift[1]);
    end;
  If (S and kbCtrl)<>0 Then
    AddToString(T,SShift[2]);
  If (S and kbAlt)<>0 Then
    AddToString(T,SShift[3]);
  ShiftStateToString:=T;
end;

Function FunctionKeyName (KeyCode : Word) : String;

begin
  If ((KeyCode-KbdF1)<$1F) Then
    FunctionKeyName:='F'+IntToStr((KeyCode-KbdF1+1))
  else
    begin
    If (KeyCode-kbdHome)<($2F-$1F) then
      FunctionKeyName:=SKeyPad[(KeyCode-kbdHome)]
    else
      FunctionKeyName:=SUnknownFunctionKey + IntToStr(KeyCode);
    end;
end;

Function KeyEventToString(KeyEvent : TKeyEvent) : String;

Var
  T : String;

begin
  T:=ShiftStateToString(KeyEvent,False);
  Case GetKeyEventFlags(KeyEvent) of
    kbASCII   : AddToString(T,GetKeyEventChar(KeyEvent));
    kbUniCode : AddToString(T,SUniCodeChar+IntToStr(GetKeyEventUniCode(Keyevent)));
    kbFnKey   : AddToString(T,FunctionKeyName(GetKeyEventCode(KeyEvent)));
                // Not good, we need a GetKeyEventScanCode function !!
    kbPhys    : AddToString(T,SScanCode+IntToStr(KeyEvent and $ffff));
  end;
  KeyEventToString:=T;
end;


const
  PrevCtrlBreakHandler: TCtrlBreakHandler = nil;

function KbdCtrlBreakHandler (CtrlBreak: boolean): boolean;
begin
(* Earlier registered handlers (user specific) have priority. *)
  if Assigned (PrevCtrlBreakHandler) then
    if PrevCtrlBreakHandler (CtrlBreak) then
      begin
        KbdCtrlBreakHandler := true;
        Exit;
      end;
(* If Ctrl-Break was pressed, either ignore it or allow default processing. *)
  if CtrlBreak then
    KbdCtrlBreakHandler := false
  else (* Ctrl-C pressed or not possible to distinguish *)
    begin
      PutKeyEvent ((kbCtrl shl 16) or 3);
      KbdCtrlBreakHandler := true;
    end;
end;


procedure SetKbdCtrlBreakHandler;
begin
  PrevCtrlBreakHandler := SysSetCtrlBreakHandler (@KbdCtrlBreakHandler);
  if PrevCtrlBreakHandler = TCtrlBreakHandler (pointer (-1)) then
    PrevCtrlBreakHandler := nil;
end;