-
Notifications
You must be signed in to change notification settings - Fork 7
/
uGlobalHotKeys.pas
57 lines (49 loc) · 1.62 KB
/
uGlobalHotKeys.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
/// <summary>
/// Working with global system hotkeys
/// </summary>
unit uGlobalHotKeys;
interface
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, ExtCtrls, TlHelp32, mmsystem,
Menus, Spin;
/// <summary>
/// Register global hotkey by code retrieved from TShortCut (THotKey) and window handle
/// </summary>
function RegisterGlobalHotKey(HotKeyFromTHotkey: TShortCut; GlobalAtomName: string;
AppWindowHandle: HWND): Word;
/// <summary>
/// Remove global hotkey by it's id and window handle
/// </summary>
procedure RemoveGlobalHotKey(HkID: Word; AppWindowHandle: HWND);
implementation
function RegisterGlobalHotKey(HotKeyFromTHotkey: TShortCut; GlobalAtomName: string;
AppWindowHandle: HWND): Word;
procedure ShortCutToHotKey(HotKey: TShortCut; var Key: Word; var Modifiers: Uint);
// THotKey id -> WinApi id
var
Shift: TShiftState;
begin
ShortCutToKey(HotKey, Key, Shift);
Modifiers := 0;
if (ssShift in Shift) then
Modifiers := Modifiers or MOD_SHIFT;
if (ssAlt in Shift) then
Modifiers := Modifiers or MOD_ALT;
if (ssCtrl in Shift) then
Modifiers := Modifiers or MOD_CONTROL;
end;
var
Key: Word;
Modifiers: Uint;
begin
ShortCutToHotKey(HotKeyFromTHotkey, Key, Modifiers);
Result := GlobalAddAtom(PChar(GlobalAtomName)); // "atomic" HK_ID
RegisterHotKey(AppWindowHandle, Result, Modifiers, Key);
end;
procedure RemoveGlobalHotKey(HkID: Word; AppWindowHandle: HWND);
begin
GlobalDeleteAtom(HkID);
UnRegisterHotKey(AppWindowHandle, HkID);
end;
end.