-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
hotkeyhelper.pas
392 lines (349 loc) · 10.9 KB
/
hotkeyhelper.pas
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
unit hotkeyhelper;
interface
uses
Windows, Classes, SysUtils, Messages, IniFiles, Generics.Collections;
const
WH_KEYBOARD_LL = 13;
WH_MOUSE_LL = 14;
{ Define a record for recording and passing information process wide }
type
PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
TKBDLLHOOKSTRUCT = record
vkCode: Cardinal;
scanCode: Cardinal;
flags: Cardinal;
time: Cardinal;
dwExtrainfo: Cardinal;
end;
type
THotkeyInvoker = class
private
FHook: HHOOK;
FBlockInput: Boolean;
class var FInstance: THotkeyInvoker;
procedure SetBlockInput(const value: Boolean);
class function GetInstance: THotkeyInvoker; static;
protected
constructor Create;
destructor Destroy; override;
public
class property Instance: THotkeyInvoker read GetInstance;
property BlockInput: Boolean read FBlockInput write SetBlockInput;
function InvokeHotKey(const Hotkey: string; Delay: Integer = 10; RepeatCount: Integer = 1): Boolean;
end;
function LowLevelKeyboardProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
implementation
var
BlockedKeys: TStringList;
VKMap: TDictionary<string,Byte>;
// Helper functions
function StartsWith(const Str, Prefix: string): Boolean;
begin
Result := (Length(Str) >= Length(Prefix)) and
(Copy(Str, 1, Length(Prefix)) = Prefix);
end;
function EndsWith(const Str, Suffix: string): Boolean;
begin
Result := (Length(Str) >= Length(Suffix))and
(Copy(Str, Length(Str) - Length(Suffix) + 1, Length(Suffix)) = Suffix);
end;
function TrimUnderscores(const Str: string): string;
var
StartPos, EndPos: Integer;
begin
StartPos := 1;
EndPos := Length(Str);
while (StartPos <= EndPos) and (Str[StartPos] = '_') do
Inc(StartPos);
while (EndPos >= StartPos) and (Str[EndPos] = '_') do
Dec(EndPos);
Result := Copy(Str, StartPos, EndPos - StartPos + 1);
end;
function LowLevelKeyboardProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
pkbhs: PKBDLLHOOKSTRUCT;
begin
if (nCode = HC_ACTION) and THotkeyInvoker.Instance.BlockInput then
begin
pkbhs := PKBDLLHOOKSTRUCT(lParam);
if BlockedKeys.IndexOf(IntToStr(pkbhs.vkCode)) <> -1 then
begin
// Result := 1;
// Exit;
end;
end;
Result := CallNextHookEx(THotKeyInvoker.Instance.FHook, nCode, wParam, lParam);
end;
{ THotkeyInvoker }
constructor THotkeyInvoker.Create;
begin
inherited;
FHook := SetWindowsHookEx(WH_KEYBOARD_LL, @LowLevelKeyboardproc, 0, 0);
BlockedKeys := TStringList.Create;
VKMap := TDictionary<string,Byte>.Create;
// allowed virtual keys
VKMap.Add('LBUTTON',VK_LBUTTON );
VKMap.Add('RBUTTON',VK_RBUTTON );
VKMap.Add('CANCEL',VK_CANCEL );
VKMap.Add('MBUTTON',VK_MBUTTON );
VKMap.Add('XBUTTON1',VK_XBUTTON1 );
VKMap.Add('XBUTTON2',VK_XBUTTON2 );
VKMap.Add('BACK',VK_BACK );
VKMap.Add('TAB',VK_TAB );
VKMap.Add('CLEAR',VK_CLEAR );
VKMap.Add('RETURN',VK_RETURN );
VKMap.Add('SHIFT',VK_SHIFT );
VKMap.Add('CONTROL',VK_CONTROL );
// VKMap.Add('MENU',VK_MENU );
VKMap.Add('ALT',VK_MENU );
VKMap.Add('PAUSE',VK_PAUSE );
VKMap.Add('CAPITAL',VK_CAPITAL );
VKMap.Add('KANA',VK_KANA );
VKMap.Add('HANGUL',VK_HANGUL );
VKMap.Add('JUNJA',VK_JUNJA );
VKMap.Add('FINAL',VK_FINAL );
VKMap.Add('HANJA',VK_HANJA );
VKMap.Add('KANJI',VK_KANJI );
VKMap.Add('CONVERT',VK_CONVERT );
VKMap.Add('NONCONVERT',VK_NONCONVERT );
VKMap.Add('ACCEPT',VK_ACCEPT );
VKMap.Add('MODECHANGE',VK_MODECHANGE );
VKMap.Add('ESCAPE',VK_ESCAPE );
VKMap.Add('SPACE',VK_SPACE );
VKMap.Add('PRIOR',VK_PRIOR );
VKMap.Add('NEXT',VK_NEXT );
VKMap.Add('END',VK_END );
VKMap.Add('HOME',VK_HOME );
VKMap.Add('LEFT',VK_LEFT );
VKMap.Add('UP',VK_UP );
VKMap.Add('RIGHT',VK_RIGHT );
VKMap.Add('DOWN',VK_DOWN );
VKMap.Add('SELECT',VK_SELECT );
VKMap.Add('PRINT',VK_PRINT );
VKMap.Add('EXECUTE',VK_EXECUTE );
VKMap.Add('SNAPSHOT',VK_SNAPSHOT );
VKMap.Add('INSERT',VK_INSERT );
VKMap.Add('DELETE',VK_DELETE );
VKMap.Add('HELP',VK_HELP );
VKMap.Add('WIN',VK_LWIN );
VKMap.Add('RWIN',VK_RWIN );
VKMap.Add('APPS',VK_APPS );
VKMap.Add('SLEEP',VK_SLEEP );
VKMap.Add('NUMPAD0',VK_NUMPAD0 );
VKMap.Add('NUMPAD1',VK_NUMPAD1 );
VKMap.Add('NUMPAD2',VK_NUMPAD2 );
VKMap.Add('NUMPAD3',VK_NUMPAD3 );
VKMap.Add('NUMPAD4',VK_NUMPAD4 );
VKMap.Add('NUMPAD5',VK_NUMPAD5 );
VKMap.Add('NUMPAD6',VK_NUMPAD6 );
VKMap.Add('NUMPAD7',VK_NUMPAD7 );
VKMap.Add('NUMPAD8',VK_NUMPAD8 );
VKMap.Add('NUMPAD9',VK_NUMPAD9 );
VKMap.Add('MULTIPLY',VK_MULTIPLY );
VKMap.Add('ADD',VK_ADD );
VKMap.Add('SEPARATOR',VK_SEPARATOR );
VKMap.Add('SUBTRACT',VK_SUBTRACT );
VKMap.Add('DECIMAL',VK_DECIMAL );
VKMap.Add('DIVIDE',VK_DIVIDE );
VKMap.Add('F1',VK_F1 );
VKMap.Add('F2',VK_F2 );
VKMap.Add('F3',VK_F3 );
VKMap.Add('F4',VK_F4 );
VKMap.Add('F5',VK_F5 );
VKMap.Add('F6',VK_F6 );
VKMap.Add('F7',VK_F7 );
VKMap.Add('F8',VK_F8 );
VKMap.Add('F9',VK_F9 );
VKMap.Add('F10',VK_F10 );
VKMap.Add('F11',VK_F11 );
VKMap.Add('F12',VK_F12 );
VKMap.Add('F13',VK_F13 );
VKMap.Add('F14',VK_F14 );
VKMap.Add('F15',VK_F15 );
VKMap.Add('F16',VK_F16 );
VKMap.Add('F17',VK_F17 );
VKMap.Add('F18',VK_F18 );
VKMap.Add('F19',VK_F19 );
VKMap.Add('F20',VK_F20 );
VKMap.Add('F21',VK_F21 );
VKMap.Add('F22',VK_F22 );
VKMap.Add('F23',VK_F23 );
VKMap.Add('F24',VK_F24 );
VKMap.Add('NUMLOCK',VK_NUMLOCK );
VKMap.Add('SCROLL',VK_SCROLL );
//VK_L & VK_R - left and right Alt, Ctrl and Shift virtual keys.
//Used only as parameters to GetAsyncKeyState() and GetKeyState().);
//No other API or message will distinguish left and right keys in this way. });
VKMap.Add('LSHIFT',VK_LSHIFT );
VKMap.Add('RSHIFT',VK_RSHIFT );
VKMap.Add('LCONTROL',VK_LCONTROL );
VKMap.Add('RCONTROL',VK_RCONTROL );
VKMap.Add('LMENU',VK_LMENU );
VKMap.Add('RMENU',VK_RMENU );
VKMap.Add('BROWSER_BACK',VK_BROWSER_BACK );
VKMap.Add('BROWSER_FORWARD',VK_BROWSER_FORWARD );
VKMap.Add('BROWSER_REFRESH',VK_BROWSER_REFRESH );
VKMap.Add('BROWSER_STOP',VK_BROWSER_STOP );
VKMap.Add('BROWSER_SEARCH',VK_BROWSER_SEARCH );
VKMap.Add('BROWSER_FAVORITES',VK_BROWSER_FAVORITES );
VKMap.Add('BROWSER_HOME',VK_BROWSER_HOME );
VKMap.Add('VOLUME_MUTE',VK_VOLUME_MUTE );
VKMap.Add('VOLUME_DOWN',VK_VOLUME_DOWN );
VKMap.Add('VOLUME_UP',VK_VOLUME_UP );
VKMap.Add('MEDIA_NEXT_TRACK',VK_MEDIA_NEXT_TRACK );
VKMap.Add('MEDIA_PREV_TRACK',VK_MEDIA_PREV_TRACK );
VKMap.Add('MEDIA_STOP',VK_MEDIA_STOP );
VKMap.Add('MEDIA_PLAY_PAUSE',VK_MEDIA_PLAY_PAUSE );
VKMap.Add('LAUNCH_MAIL',VK_LAUNCH_MAIL );
VKMap.Add('LAUNCH_MEDIA_SELECT',VK_LAUNCH_MEDIA_SELECT );
VKMap.Add('LAUNCH_APP1',VK_LAUNCH_APP1 );
VKMap.Add('LAUNCH_APP2',VK_LAUNCH_APP2 );
VKMap.Add('OEM_1',VK_OEM_1 );
VKMap.Add('OEM_PLUS',VK_OEM_PLUS );
VKMap.Add('OEM_COMMA',VK_OEM_COMMA );
VKMap.Add('OEM_MINUS',VK_OEM_MINUS );
VKMap.Add('OEM_PERIOD',VK_OEM_PERIOD );
VKMap.Add('OEM_2',VK_OEM_2 );
VKMap.Add('OEM_3',VK_OEM_3 );
VKMap.Add('OEM_4',VK_OEM_4 );
VKMap.Add('OEM_5',VK_OEM_5 );
VKMap.Add('OEM_6',VK_OEM_6 );
VKMap.Add('OEM_7',VK_OEM_7 );
VKMap.Add('OEM_8',VK_OEM_8 );
VKMap.Add('OEM_102',VK_OEM_102 );
VKMap.Add('PACKET',VK_PACKET );
VKMap.Add('PROCESSKEY',VK_PROCESSKEY );
VKMap.Add('ATTN',VK_ATTN );
VKMap.Add('CRSEL',VK_CRSEL );
VKMap.Add('EXSEL',VK_EXSEL );
VKMap.Add('EREOF',VK_EREOF );
VKMap.Add('PLAY',VK_PLAY );
VKMap.Add('ZOOM',VK_ZOOM );
VKMap.Add('NONAME',VK_NONAME );
VKMap.Add('PA1',VK_PA1 );
VKMap.Add('OEM_CLEAR',VK_OEM_CLEAR);
end;
destructor THotkeyInvoker.Destroy;
begin
if FHook <> 0 then
UnhookWindowsHookEx(FHook);
VKMap.Clear;
VKMap.Free;
BlockedKeys.Free;
inherited;
end;
class function THotkeyInvoker.GetInstance: THotkeyInvoker;
begin
if FInstance = nil then
FInstance := THotkeyInvoker.Create;
Result := FInstance;
end;
function THotkeyInvoker.InvokeHotKey(const Hotkey: string; Delay,
RepeatCount: Integer): Boolean;
var
Keys: TStringList;
Key: string;
VK: Byte;
IsHoldDown, IsRelease: Boolean;
I, J: Integer;
HeldKeys: TList;
procedure SendKey(VirtualKey: Byte; Flag: DWORD);
begin
keybd_event(VirtualKey, MapVirtualKey(VirtualKey, 0), Flag, 0);
Sleep(Delay);
end;
function GetVirtualKey(const Key: string): Byte;
var
VK: Byte;
begin
if VKMap.TryGetValue(Key,VK) then
Result := VK
else
Result := Ord(Key[1]);
end;
begin
Result := False;
Keys := TStringList.Create;
HeldKeys := TList.Create;
try
Keys.Delimiter := '+';
Keys.DelimitedText := UpperCase(HotKey);// StringReplace(Hotkey, '\+', #1, [rfReplaceAll]);
// for I := 0 to Keys.Count - 1 do
// Keys[I] := StringReplace(Keys[I], #1, '+', [rfReplaceAll]);
BlockedKeys.Clear;
for I := 0 to Keys.Count - 1 do
begin
Key := Keys[I];
IsHoldDown := StartsWith(Key, '_');
IsRelease := EndsWith(Key, '_');
Key := TrimUnderscores(Key);
// if Key = 'CTRL' then VK := VK_CONTROL
// else if Key = 'ALT' then VK := VK_MENU
// else if Key = 'SHIFT' then VK := VK_SHIFT
// else if Key = 'WIN' then VK := VK_LWIN
// else if Key = 'TAB' then VK := VK_TAB
// else VK := Ord(UpperCase(Key)[1]);
VK := GetVirtualKey(Key);
BlockedKeys.Add(IntToStr(VK));
end;
BlockInput := True;
try
for J := 1 to RepeatCount do
begin
for I := 0 to Keys.Count - 1 do
begin
Key := Keys[I];
IsHoldDown := StartsWith(Key, '_');
IsRelease := EndsWith(Key, '_');
Key := TrimUnderscores(Key);
// if Key = 'CTRL' then VK := VK_CONTROL
// else if Key = 'ALT' then VK := VK_MENU
// else if Key = 'SHIFT' then VK := VK_SHIFT
// else if Key = 'WIN' then VK := VK_LWIN
// else if Key = 'TAB' then VK := VK_TAB
// else VK := Ord(UpperCase(Key)[1]);
VK := GetVirtualKey(Key);
if IsHoldDown and not IsRelease then
begin
SendKey(VK, 0);
HeldKeys.Add(Pointer(VK));
end
else if not IsHoldDown and not IsRelease then
begin
SendKey(VK, 0);
SendKey(VK, KEYEVENTF_KEYUP);
end
else if IsRelease then
begin
if HeldKeys.IndexOf(Pointer(VK)) >= 0 then
HeldKeys.Remove(Pointer(VK));
SendKey(VK, KEYEVENTF_KEYUP);
end;
end;
// Release any remaining held keys in reverse order
for I := HeldKeys.Count - 1 downto 0 do
begin
SendKey(Byte(HeldKeys[I]), KEYEVENTF_KEYUP);
end;
HeldKeys.Clear;
if J < RepeatCount then
Sleep(Delay);
end;
Result := True;
finally
BlockInput := False;
end;
finally
HeldKeys.Free;
Keys.Free;
end;
end;
procedure THotkeyInvoker.SetBlockInput(const value: Boolean);
begin
FBlockInput := Value;
end;
initialization
THotkeyInvoker.FInstance := THotkeyInvoker.Create;
finalization
FreeAndNil(THotkeyInvoker.FInstance);
end.