-
Notifications
You must be signed in to change notification settings - Fork 7
/
FormHotkeys.pas
143 lines (117 loc) · 3.33 KB
/
FormHotkeys.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
unit FormHotkeys;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls,
Vcl.ExtCtrls;
type
THotkeyForm = class(TForm)
hkHotkey: THotKey;
Label1: TLabel;
pnlOK: TPanel;
procedure pnlOKClick(Sender: TObject);
private
public
property hotKey: THotKey read hkHotKey;
end;
function checkHotkey(aWND: HWND): boolean;
var
FHotkeyEnabled: boolean;
FHotkeyAtom: integer;
implementation
uses
vcl.menus, customMenuCommon, _debugWindow;
const
CM_HOTKEY_FILE_NAME = 'hotkey';
var
FHotkeyWND: HWND;
FHotKey: integer;
FShift: THKModifiers;
{$R *.dfm}
function SetToInt(const aSet; const Size: integer): integer;
begin
Result := 0;
Move(aSet, Result, Size);
end;
procedure IntToSet(const Value:integer; var aSet; const Size: integer);
begin
Move(Value, aSet, Size);
end;
function readHotkey: boolean;
begin
result := FALSE;
FHotkey := 0;
FShift := [];
var sl := TStringList.create;
try
sl.loadFromFile(getExePath + CM_HOTKEY_FILE_NAME);
case sl.count = 0 of TRUE: EXIT; end;
FHotkey := strToIntDef(sl[0], 0);
case sl.count > 1 of TRUE: intToSet(strToIntDef(sl[1], 0), FShift, sizeOf(FShift)); end;
result := TRUE;
finally
sl.free;
end;
end;
function shortcutAsKey(aHotkey: integer): integer;
var
key: WORD;
dummyShift: TShiftState;
begin
ShortCutToKey(aHotkey, key, dummyShift);
result := key;
end;
function registerHotkey(aHotkey: integer; aShift: THKModifiers): boolean;
var
key, mods: WORD;
begin
result := FALSE;
case FHotkeyEnabled of TRUE: EXIT; end;
FHotkeyAtom := globalAddAtom('CustomMenuHotkey');
key := shortcutAsKey(aHotkey);
mods := 0;
case hkCtrl in aShift of TRUE: mods := mods or MOD_CONTROL; end;
case hkShift in aShift of TRUE: mods := mods or MOD_SHIFT; end;
case hkAlt in aShift of TRUE: mods := mods or MOD_ALT; end;
FHotkeyEnabled := winAPI.windows.RegisterHotKey(FHotkeyWnd, FHotkeyAtom, mods, key);
result := TRUE;
end;
function checkHotkey(aWND: HWND): boolean;
var
hotkeyForm: THotkeyForm;
begin
case fileExists(getExePath + CM_HOTKEY_FILE_NAME) of FALSE: EXIT; end;
FHotkeyWnd := aWND;
case readHotkey of TRUE: registerHotKey(FHotkey, FShift);
FALSE: begin
hotkeyForm := THotkeyForm.create(NIL);
try
hotkeyForm.showModal;
finally
hotkeyForm.free;
end;end;end;
end;
function saveHotkey(aHotkey: integer; aShift: THKModifiers): boolean;
begin
var sl := TStringList.create;
try
sl.add(intToStr(shortcutAsKey(aHotkey)));
case aShift <> [] of TRUE: sl.add(intToStr(setToInt(aShift, sizeOf(aShift)))); end;
sl.saveToFile(getExePath + CM_HOTKEY_FILE_NAME);
finally
sl.free;
end;
end;
{ THotkeyForm }
procedure THotkeyForm.pnlOKClick(Sender: TObject);
begin
registerHotkey(hotkey.hotkey, hotkey.Modifiers);
saveHotkey(hotkey.hotkey, hotkey.Modifiers);
modalResult := mrOK;
end;
initialization
FHotkeyEnabled := FALSE;
FHotkeyAtom := 0;
finalization
case FHotkeyEnabled of TRUE: winAPI.windows.UnregisterHotKey(FHotKeyWnd, FHotkeyAtom); end;
end.