summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/old/hotkeys.pas
blob: 32f6e201e53ba6146fd101affccfc6ed16b4d0c7 (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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
{Hotkey and timer related functions}
unit hotkeys;

interface

uses windows;

type
  AWKHotKeyProc = function(hotkey:integer):integer;

function AddProc(aproc:AWKHotKeyProc;ahotkey:integer;global:bool=false):integer; overload;
function AddProc(ahotkey:integer;wnd:HWND;aproc:AWKHotKeyProc         ):integer; overload;
function AddProc(ahotkey:integer;wnd:HWND;msg:uint_ptr                ):integer; overload;
function DelProc(hotkey:integer         ):integer;  overload;
function DelProc(hotkey:integer;wnd:HWND):integer; overload;

procedure InitHotKeys;
procedure FreeHotKeys;

implementation

uses messages;

const
  HWND_MESSAGE = HWND(-3);
  
var
  CurThread:THANDLE;

type
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;
  TKBDLLHOOKSTRUCT = record
    vkCode     :dword;
    scanCode   :dword;
    flags      :dword;
    time       :dword;
    dwExtraInfo:dword;
  end;

const
  WH_KEYBOARD_LL = 13;
  WM_MYMESSAGE = WM_USER +13;

// const from commctrl module;
const
  HOTKEYF_SHIFT   = $01;
  HOTKEYF_CONTROL = $02;
  HOTKEYF_ALT     = $04;
  HOTKEYF_EXT     = $08;

const
  hkAssigned = 1;
  hkGlobal   = 2;
  hkMessage  = 4;
const
  kbHook:THANDLE=0;
  hiddenwindow:HWND=0;
  modifiers:dword=0;
const
  PageStep = 10;
type
  PHKRec = ^THKRec;
  THKRec = record
    proc  :AWKHotKeyProc; // procedure
    flags :integer;       // options
    handle:THANDLE;       // thread or window?
    atom  :TATOM;         // hotkey id
    hotkey:integer;       // hotkey
  end;
  PHKRecs = ^THKRecs;
  THKRecs = array [0..15] of THKRec;

const
  NumRecs:integer=0;
  MaxRecs:integer=10;
  hkRecs:pHKRecs=nil;

//----- simpler version of 'common' function -----

const
  HexDigitChr: array [0..15] of AnsiChar = ('0','1','2','3','4','5','6','7',
                                        '8','9','A','B','C','D','E','F');

function IntToHex(dst:PAnsiChar;Value:cardinal):PAnsiChar;
var
  Digits:integer;
begin
  dst[8]:=#0;
  Digits:=8;
  repeat
    Dec(Digits);
    dst[Digits]:=HexDigitChr[Value and $F];
    Value:=Value shr 4;
  until Digits=0;
  result:=dst;
end;

//----- utils -----

function GetAtom(hotkey:dword):dword;
const
  HKPrefix = 'awk_';
var
  p:array [0..15] of AnsiChar;
begin
  lstrcpya(p,HKPrefix);
  IntToHex(p+Length(HKPrefix),hotkey);
  result:=GlobalAddAtomA(p);
end;

function HotKeyDlgToHook(w:cardinal):cardinal; register;
asm
  movzx ecx,al
  xor   al,al
  test  ah,HOTKEYF_ALT
  je    @L1
  or    al,MOD_ALT
@L1:
  test ah,HOTKEYF_CONTROL
  je   @L2
  or   al,MOD_CONTROL
@L2:
  test ah,HOTKEYF_SHIFT
  je   @L3
  or   al,MOD_SHIFT
@L3:
  test ah,HOTKEYF_EXT
  je   @L4
  or   al,MOD_WIN
@L4:
  mov ch,al
  mov eax,ecx
{
begin
  result:=w and $FF;
  if (w and (HOTKEYF_ALT     shl 8))<>0 then result:=result or (MOD_ALT     shl 8);
  if (w and (HOTKEYF_CONTROL shl 8))<>0 then result:=result or (MOD_CONTROL shl 8);
  if (w and (HOTKEYF_SHIFT   shl 8))<>0 then result:=result or (MOD_SHIFT   shl 8);
  if (w and (HOTKEYF_EXT     shl 8))<>0 then result:=result or (MOD_WIN     shl 8);
}
end;

function HotKeyHookToDlg(w:cardinal):cardinal; register;
asm
  movzx ecx,al
  xor   al,al
  test  ah,MOD_ALT
  je    @L1
  or    al,HOTKEYF_ALT
@L1:
  test ah,MOD_CONTROL
  je   @L2
  or   al,HOTKEYF_CONTROL
@L2:
  test ah,MOD_SHIFT
  je   @L3
  or   al,HOTKEYF_SHIFT
@L3:
  test ah,MOD_WIN
  je   @L4
  or   al,HOTKEYF_EXT
@L4:
  mov  ch,al
  mov  eax,ecx
{
begin
  result:=w and $FF;
  if (w and (MOD_ALT     shl 8))<>0 then result:=result or (HOTKEYF_ALT     shl 8);
  if (w and (MOD_CONTROL shl 8))<>0 then result:=result or (HOTKEYF_CONTROL shl 8);
  if (w and (MOD_SHIFT   shl 8))<>0 then result:=result or (HOTKEYF_SHIFT   shl 8);
  if (w and (MOD_WIN     shl 8))<>0 then result:=result or (HOTKEYF_EXT     shl 8);
}
end;

//----- Hook -----

function FindHotkey(keycode:integer;local:boolean):pointer;
var
  i:integer;
  p:pHKRec;
begin
  i:=NumRecs;
  p:=pointer(HKRecs);
  while i>0 do
  begin
    dec(i);
    with p^ do
    begin
      if (flags and hkAssigned)<>0 then
      begin
        if (local xor ((flags and hkGlobal)<>0)) then
        begin
          if hotkey=keycode then
          begin
            if handle<>0 then
            begin
              if GetFocus=handle then
              begin
                if (flags and hkMessage)<>0 then
                begin
                  PostMessage(handle,wparam(@proc),keycode,0);
                  result:=pointer(-1);
                end
                else
                  result:=@proc;
                exit;
              end;
            end
            else
            begin
              result:=@proc;
              exit;
            end;
          end;
        end;
      end;
    end;
    inc(p);
  end;
  result:=nil;
end;

function wmKeyboard_hook(code:integer;wParam:WPARAM;lParam:LPARAM):longint; stdcall;
var
  key:dword;
  proc:pointer;
begin
  if (code=HC_ACTION) and
     (lParam>0) and (LoWord(lParam)=1) then
  begin
    key:=0;
    if (GetKeyState(VK_SHIFT  ) and $8000)<>0 then key:=key or (MOD_SHIFT   shl 8);
    if (GetKeyState(VK_CONTROL) and $8000)<>0 then key:=key or (MOD_CONTROL shl 8);
    if (GetKeyState(VK_MENU   ) and $8000)<>0 then key:=key or (MOD_ALT     shl 8);
    if (GetKeyState(VK_LWIN   ) and $8000)<>0 then key:=key or (MOD_WIN     shl 8);
    if (GetKeyState(VK_RWIN   ) and $8000)<>0 then key:=key or (MOD_WIN     shl 8);
//    if (GetKeyState(VK_APPS) and $8000)<>0 then
//    if (GetKeyState(VK_SLEEP) and $8000)<>0 then
    key:=key or (cardinal(wParam) and $FF);
    proc:=FindHotkey(key,true);
    if proc<>nil then
    begin
      if proc<>pointer(-1) then
        PostMessageA(hiddenwindow,WM_MYMESSAGE,key,windows.lparam(proc));
      result:=1;
      exit;
    end;
  end;
  result:=CallNextHookEx(KbHook,code,wParam,lParam);
end;

function wmKeyboardLL_hook(code:integer;wParam:WPARAM;lParam:LPARAM):integer; stdcall;
const
  lastkey:dword=0;
var
  mask:dword;
  key:dword;
  proc:pointer;
begin
  if code=HC_ACTION then
  begin
    case PKBDLLHOOKSTRUCT(lParam)^.vkCode of
      VK_MENU,
      VK_LMENU,
      VK_RMENU:    mask:=MOD_ALT     shl 8;
      VK_LWIN,
      VK_RWIN:     mask:=MOD_WIN     shl 8;
      VK_SHIFT,
      VK_LSHIFT,
      VK_RSHIFT:   mask:=MOD_SHIFT   shl 8;
      VK_CONTROL,
      VK_LCONTROL,
      VK_RCONTROL: mask:=MOD_CONTROL shl 8;
    else
      if (PKBDLLHOOKSTRUCT(lParam)^.flags and 128)=0 then
      begin
        // local only
// maybe process will better choice?
        if //(lastkey=0) and
           (CurThread=GetWindowThreadProcessId(GetForegroundWindow,nil)) then
        begin
          key:=PKBDLLHOOKSTRUCT(lParam)^.vkCode or modifiers;
          proc:=FindHotkey(key,true);
          if proc<>nil then
          begin
            lastkey:=PKBDLLHOOKSTRUCT(lParam)^.vkCode;
            if proc<>pointer(-1) then
              PostMessageA(hiddenwindow,WM_MYMESSAGE,key,windows.lparam(proc));
            result:=1;
            exit;
          end;
        end;
      end
      else if (lastkey<>0) and (lastkey=PKBDLLHOOKSTRUCT(lParam)^.vkCode) then
      begin
        lastkey:=0;
        result :=1;
        exit;
      end;
      mask:=0;
    end;
    if mask<>0 then
    begin
      if (PKBDLLHOOKSTRUCT(lParam)^.flags and 128)=0 then
        modifiers:=modifiers or mask
      else
        modifiers:=modifiers and not mask;
    end
  end;
  result:=CallNextHookEx(KbHook,code,wParam,lParam);
end;

function HiddenWindProc(wnd:HWnd;msg:UINT;wParam:WPARAM;lParam:LPARAM):lresult; stdcall;
var
  key:dword;
begin
  if Msg=WM_HOTKEY then
  begin
    key:=(lParam shr 16)+((lParam and $FF) shl 8);
    result:=lresult(FindHotKey(key,false));
    if result<>0 then
    begin
      result:=AWKHotKeyProc(result)(HotkeyHookToDlg(key));
      exit;
    end;
  end
  else if Msg=WM_MYMESSAGE then
  begin
    result:=AWKHotKeyProc(lParam)(HotkeyHookToDlg(wParam));
    exit;
  end;
  result:=DefWindowProcA(wnd,msg,wparam,lparam);
end;

procedure DestroyHiddenWindow;
begin
  if hiddenwindow<>0 then
  begin
    DestroyWindow(hiddenwindow);
    hiddenwindow:=0;
  end;
end;

procedure CreateHiddenWindow;
var
  wnd:HWND;
begin
  if hiddenwindow=0 then
  begin
    wnd:=CreateWindowExA(0,'STATIC',nil,0,
       1,1,1,1,HWND_MESSAGE,0,hInstance,nil);
    if wnd<>0 then
    begin
      SetWindowLongPtrA(wnd,GWL_WNDPROC,LONG_PTR(@HiddenWindProc));
      hiddenwindow:=wnd;
    end
  end
end;
//----- interface -----

function CheckTable(ahotkey:integer;global:bool):integer;
var
  tmp:pHKRecs;
  i:integer;
  p:pHKRec;
begin
  if HKRecs=nil then
  begin
    MaxRecs:=PageStep;
    GetMem  (HKRecs ,MaxRecs*SizeOf(THKRec));
    FillChar(HKRecs^,MaxRecs*SizeOf(THKRec),0);
    NumRecs:=0;
  end;
  // search existing
  i:=0;
  p:=pointer(HKRecs);
  while i<NumRecs do
  begin
    if (p^.flags and hkAssigned)<>0 then
    begin
      if (p^.hotkey=ahotkey) and
         (((p^.flags and hkGlobal)<>0) xor not global) then
        break;
    end;
    inc(p);
    inc(i);
  end;
  //search empty
  if i=NumRecs then
  begin
    i:=0;
    p:=pointer(HKRecs);
    while i<NumRecs do
    begin
      if (p^.flags and hkAssigned)=0 then
        break;
      inc(p);
      inc(i);
    end;
  end;
  if i=NumRecs then // allocate if not found
  begin
    if NumRecs=MaxRecs then
    begin
      inc(MaxRecs,PageStep);
      GetMem  (tmp ,MaxRecs*SizeOf(THKRec));
      FillChar(tmp^,MaxRecs*SizeOf(THKRec),0);
      move(HKRecs^,tmp^,NumRecs*SizeOf(THKRec));
      FreeMem(HKRecs);
      HKRecs:=tmp;
    end;
    inc(NumRecs);
  end;
  if global then
    HKRecs^[i].flags:=hkAssigned or hkGlobal
  else
    HKRecs^[i].flags:=hkAssigned;
  HKRecs^[i].hotkey:=HotKeyDlgToHook(ahotkey);
  result:=i;
end;

function AddProc(aproc:AWKHotKeyProc;ahotkey:integer;global:bool=false):integer;
begin
  result:=1;
  if @aproc=nil then exit;

  with HKRecs^[CheckTable(ahotkey,global)] do
  begin
    proc  :=aproc;
    handle:=0;
    if global then
    begin
      atom:=GetAtom(hotkey);
      if not RegisterHotKey(hiddenwindow,atom,((hotkey and $FF00) shr 8),(hotkey and $FF)) then
        result:=0;
    end;
  end;
end;

// search needed
function AddProcWin(ahotkey:integer;wnd:HWND):integer;
begin
  result:=CheckTable(ahotkey,false);
  with HKRecs^[result] do
  begin
    handle:=wnd;
  end;
end;

function AddProc(ahotkey:integer;wnd:HWND;aproc:AWKHotKeyProc):integer;
begin
  if @aproc=nil then
  begin
    result:=0;
    exit;
  end;

  result:=AddProcWin(ahotkey,wnd);
  if result<0 then
    result:=0
  else
  begin
    HKRecs^[result].proc:=@aproc;
  end;
end;

function AddProc(ahotkey:integer;wnd:HWND;msg:uint_ptr):integer;
begin
  result:=AddProcWin(ahotkey,wnd);
  if result<0 then
    result:=0
  else
  begin
    HKRecs^[result].flags:=HKRecs^[result].flags or hkMessage;
    HKRecs^[result].proc:=pointer(msg);
  end;
end;

function DelProc(hotkey:integer):integer;
var
  i:integer;
  p:pHKRec;
begin
  hotkey:=HotKeyDlgToHook(hotkey); //!!
  p:=pointer(HKRecs);
  i:=NumRecs;
  while i>0 do
  begin
    dec(i);
    if ((p^.flags and hkAssigned)<>0) and (p^.handle=0) then
      if p^.hotkey=hotkey then
      begin
        if (p^.flags and hkGlobal)<>0 then
        begin
          UnregisterHotKey(hiddenwindow,p^.atom);
          GlobalDeleteAtom(p^.atom);
        end;
        p^.flags:=p^.flags and not hkAssigned;
        result:=i;
        exit;
      end;
    inc(p);
  end;
  result:=0;
end;

function DelProc(hotkey:integer;wnd:HWND):integer;
var
  i:integer;
  p:pHKRec;
begin
  hotkey:=HotKeyDlgToHook(hotkey); //!!
  p:=pointer(HKRecs);
  i:=NumRecs;
  while i>0 do
  begin
    dec(i);
    if (p^.flags and hkAssigned)<>0 then
      if (p^.handle=wnd) {and ((p^.flags and hkGlobal)=0)} then
      begin
        if (hotkey=0) or (hotkey=p^.hotkey) then
        begin
          p^.flags:=p^.flags and not hkAssigned;
          result:=i;
          exit;
        end;
      end;
    inc(p);
  end;
  result:=0;
end;

procedure InitHotKeys;
begin
  MaxRecs:=10;
  GetMem(HKRecs,SizeOf(THKRec)*MaxRecs);
  FillChar(HKRecs^,SizeOf(THKRec)*MaxRecs,0);
  NumRecs:=0;
  CreateHiddenWindow;
  kbhook:=SetWindowsHookExA(WH_KEYBOARD_LL,@wmKeyboardLL_hook,hInstance,0);

  if KbHook=0 then
    KbHook:=SetWindowsHookExA(WH_KEYBOARD,@wmKeyboard_hook,0,GetCurrentThreadId);
end;

procedure FreeHotKeys;
var
  i:integer;
  p:pHKRec;
begin
  i:=NumRecs;
  p:=pointer(HKRecs);
  while i>0 do
  begin
    dec(i);
    if (p^.flags and (hkAssigned or hkGlobal))=(hkAssigned or hkGlobal) then
    begin
      UnregisterHotKey(hiddenwindow,p^.atom);
      GlobalDeleteAtom(p^.atom);
    end;
    inc(p);
  end;
  DestroyHiddenWindow;
  if kbhook<>0 then
    UnhookWindowsHookEx(kbhook);
  FreeMem(HKRecs);
  HKRecs:=nil;
  MaxRecs:=0;
  NumRecs:=0;
end;

initialization
  CurThread:=GetCurrentThreadId();
end.