File: cocoakeyinput.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (255 lines) | stat: -rw-r--r-- 8,828 bytes parent folder | download | duplicates (3)
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
{ CocoaKeyInput

  Copyright (C) 2019 Sammarco Francesco

  This source is free software; you can redistribute it and/or modify it under the terms of the
  GNU General Public License as published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.

  This code 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.  See the GNU
  General Public License for more details.

  A copy of the GNU General Public License is available on the World Wide Web at
  <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit CocoaKeyInput;

{$mode objfpc}{$H+}

interface

uses
  MacOSAll,
  Classes, SysUtils, Controls, Forms,
  CocoaUtils, CGEventSource, CGEvent, CGEventTypes, CGRemoteOperation, CFBase,
  KeyInputIntf;

type

  { TCocoaKeyInput }

  TCocoaKeyInput = class(TKeyInput)
  protected
    procedure DoDown(Key: Word); override;
    procedure DoUp(Key: Word); override;
  end;

function InitializeKeyInput: TKeyInput;


implementation

uses
  LCLType;

function InitializeKeyInput: TKeyInput;
begin
  Result := TCocoaKeyInput.Create;
end;

function VKToMacCode(AKey: Word): Word;
begin
  case AKey of
    VK_A : Result :=  kVK_ANSI_A;
    VK_S : Result :=  kVK_ANSI_S;
    VK_D : Result :=  kVK_ANSI_D;
    VK_F : Result :=  kVK_ANSI_F;
    VK_H : Result :=  kVK_ANSI_H;
    VK_G : Result :=  kVK_ANSI_G;
    VK_Z : Result :=  kVK_ANSI_Z;
    VK_X : Result :=  kVK_ANSI_X;
    VK_C : Result :=  kVK_ANSI_C;
    VK_V : Result :=  kVK_ANSI_V;

    //kVK_ISO_Section   // ISO keyboard only

    VK_B : Result := kVK_ANSI_B;
    VK_Q : Result := kVK_ANSI_Q;
    VK_W : Result := kVK_ANSI_W;
    VK_E : Result := kVK_ANSI_E;
    VK_R : Result := kVK_ANSI_R;
    VK_Y : Result := kVK_ANSI_Y;
    VK_T : Result := kVK_ANSI_T;
    VK_1 : Result := kVK_ANSI_1;
    VK_2 : Result := kVK_ANSI_2;
    VK_3 : Result := kVK_ANSI_3;
    VK_4 : Result := kVK_ANSI_4;
    VK_6 : Result := kVK_ANSI_6;
    VK_5 : Result := kVK_ANSI_5;
    VK_LCL_EQUAL          : Result := kVK_ANSI_Equal; // aka VK_EQUAL = 187 = $BB;
    VK_9                  : Result := kVK_ANSI_9;
    VK_7                  : Result := kVK_ANSI_7;
    VK_OEM_MINUS          : Result := kVK_ANSI_Minus;
    VK_8                  : Result := kVK_ANSI_8;
    VK_0                  : Result := kVK_ANSI_0;
    VK_OEM_6              : Result := kVK_ANSI_RightBracket;
    VK_O                  : Result := kVK_ANSI_O;
    VK_U                  : Result := kVK_ANSI_U;
    VK_LCL_OPEN_BRAKET    : Result := kVK_ANSI_LeftBracket;
    VK_I                  : Result := kVK_ANSI_I;
    VK_P                  : Result := kVK_ANSI_P;

    VK_RETURN             : Result := kVK_Return;

    VK_L                  : Result := kVK_ANSI_L;
    VK_J                  : Result := kVK_ANSI_J;
    VK_LCL_QUOTE          : Result := kVK_ANSI_Quote;
    VK_K                  : Result := kVK_ANSI_K;
    VK_LCL_SEMI_COMMA     : Result := kVK_ANSI_Semicolon;
    VK_LCL_BACKSLASH      : Result := kVK_ANSI_Backslash;
    VK_LCL_COMMA          : Result := kVK_ANSI_Comma;
    VK_LCL_SLASH          : Result := kVK_ANSI_Slash;
    VK_N                  : Result := kVK_ANSI_N;
    VK_M                  : Result := kVK_ANSI_M;
    VK_LCL_POINT          : Result := kVK_ANSI_Period;

    VK_TAB                : Result := kVK_Tab;
    VK_SPACE              : Result := kVK_Space;

    VK_LCL_TILDE          : Result := kVK_ANSI_Grave;

    VK_BACK               : Result := kVK_Delete;
    VK_DELETE             : Result := kVK_Delete; //Added from Francesco Sammarco

    VK_ESCAPE             : Result := kVK_Escape;
    VK_LWIN               : Result := kVK_Command;
    // todo: Application.ExtendedKeysSupport must be true!

    VK_SHIFT             : Result := kVK_Shift;   //Added from Francesco Sammarco
    VK_MENU              : Result := kVK_Option;  //Added from Francesco Sammarco
    VK_CONTROL           : Result := kVK_Control; //Added from Francesco Sammarco


    VK_LSHIFT             : Result := kVK_Shift;
    VK_CAPITAL            : Result := kVK_CapsLock;
    VK_LMENU              : Result := kVK_Option;
    VK_LCONTROL           : Result := kVK_Control;
    VK_RSHIFT             : Result := kVK_RightShift;
    VK_RMENU              : Result := kVK_RightOption;
    VK_RCONTROL           : Result := kVK_RightControl;
    //kVK_Function          : Result := VK_; todo:
    VK_F17                : Result := kVK_F17;

    VK_DECIMAL              : Result := kVK_ANSI_KeypadDecimal;
    VK_MULTIPLY             : Result := kVK_ANSI_KeypadMultiply;
    VK_ADD                  : Result := kVK_ANSI_KeypadPlus;
    VK_NUMLOCK              : Result := kVK_ANSI_KeypadClear;

    VK_VOLUME_UP            : Result := kVK_VolumeUp;
    VK_VOLUME_DOWN          : Result := kVK_VolumeDown;
    VK_VOLUME_MUTE          : Result := kVK_Mute;

    VK_DIVIDE               : Result := kVK_ANSI_KeypadDivide;
    //VK_RETURN               : Result := kVK_ANSI_KeypadEnter;  //Commented from Francesco Sammarco (duplicate VK_RETURN)
    VK_SUBTRACT             : Result := kVK_ANSI_KeypadMinus;

    VK_F18         : Result := kVK_F18;
    VK_F19         : Result := kVK_F19;

    //kVK_ANSI_KeypadEquals : Result := VK_;
    VK_NUMPAD0      : Result := kVK_ANSI_Keypad0;
    VK_NUMPAD1      : Result := kVK_ANSI_Keypad1;
    VK_NUMPAD2      : Result := kVK_ANSI_Keypad2;
    VK_NUMPAD3      : Result := kVK_ANSI_Keypad3;
    VK_NUMPAD4      : Result := kVK_ANSI_Keypad4;
    VK_NUMPAD5      : Result := kVK_ANSI_Keypad5;
    VK_NUMPAD6      : Result := kVK_ANSI_Keypad6;
    VK_NUMPAD7      : Result := kVK_ANSI_Keypad7;

    VK_F20  : Result := kVK_F20;

    VK_NUMPAD8 : Result := kVK_ANSI_Keypad8;
    VK_NUMPAD9 : Result := kVK_ANSI_Keypad9;

    //kVK_JIS_Yen                   = $5D;
    //kVK_JIS_Underscore            = $5E;
    //kVK_JIS_KeypadComma           = $5F;

    VK_F5           : Result := kVK_F5;
    VK_F6           : Result := kVK_F6;
    VK_F7           : Result := kVK_F7;
    VK_F3           : Result := kVK_F3;
    VK_F8           : Result := kVK_F8;
    VK_F9           : Result := kVK_F9;

    //kVK_JIS_Eisu                  = $66;
    VK_KANA         : Result := kVK_JIS_Kana;

    VK_F11          : Result := kVK_F11;

    VK_SNAPSHOT           : Result := kVK_F13;
    VK_F16                : Result := kVK_F16;
    VK_SCROLL             : Result := kVK_F14;
    VK_F10                : Result := kVK_F10;

    VK_APPS               : Result := kVK_SubMenu;

    VK_F12                : Result := kVK_F12;
    VK_PAUSE              : Result := kVK_F15;
    VK_HELP               : Result := kVK_Help; //VK_INSERT; // todo!
    VK_HOME               : Result := kVK_Home;
    VK_PRIOR              : Result := kVK_PageUp;
    //VK_DELETE             : Result := kVK_ForwardDelete; // Commented from Francesco Sammarco
    VK_F4                 : Result := kVK_F4;
    VK_END                : Result := kVK_End;
    VK_F2                 : Result := kVK_F2;
    VK_NEXT               : Result := kVK_PageDown;
    VK_F1                 : Result := kVK_F1;
    VK_LEFT               : Result := kVK_LeftArrow;
    VK_RIGHT              : Result := kVK_RightArrow;
    VK_DOWN               : Result := kVK_DownArrow;
    VK_UP                 : Result := kVK_UpArrow;

  else
    Result := VK_UNKNOWN;
  end;
end;

procedure SendKeyInput(Key: Word; Down: Boolean);
var
  EventSourceRef  : CGEventSourceRef;
  KeyDownRef, KeyUpRef: CGEventRef;
begin

  EventSourceRef := CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
  if EventSourceRef = nil then RaiseLastOSError;
  try

    if Down then
    begin
      KeyDownRef := CGEventCreateKeyboardEvent(EventSourceRef, VKToMacCode(Key), 1); //1 = key down
      if KeyDownRef = nil then RaiseLastOSError;
      CGEventSetFlags(KeyDownRef, CGEventGetFlags(KeyDownRef)); //combine flags
      CGEventPost(kCGHIDEventTap, KeyDownRef);
    end else begin
      KeyUpRef := CGEventCreateKeyboardEvent(EventSourceRef, VKToMacCode(Key), 0);  //0 = key up
      if KeyUpRef = nil then RaiseLastOSError;
      CGEventSetFlags(KeyDownRef, CGEventGetFlags(KeyDownRef)); //combine flags
      CGEventPost(kCGHIDEventTap, KeyUpRef);
    end;

  finally
    if Down then
       if KeyDownRef <> nil then CFRelease(KeyDownRef)
    else
        if KeyUpRef <> nil then CFRelease(KeyUpRef);
    CFRelease(EventSourceRef);
  end;

end;

{ TCocoaKeyInput }
procedure TCocoaKeyInput.DoDown(Key: Word);
begin
  SendKeyInput(Key, True);
end;

procedure TCocoaKeyInput.DoUp(Key: Word);
begin
  SendKeyInput(Key, False);
end;

end.