-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHotKeyCtrl.cpp
298 lines (272 loc) · 8.6 KB
/
HotKeyCtrl.cpp
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
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2020 - Stefan Kueng
// This program 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 program 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.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "HotKeyCtrl.h"
#include <CommCtrl.h>
#define PROP_OBJECT_PTR MAKEINTATOM(ga.atom)
#define PROP_ORIGINAL_PROC MAKEINTATOM(ga.atom)
/*
* typedefs
*/
class CGlobalAtom
{
public:
CGlobalAtom(void)
{
atom = GlobalAddAtom(L"_HotKeyCtrl_Object_Pointer_"
L"\\{07B14ED2-C35E-438a-9B39-F4BAFE4E59EB}");
}
~CGlobalAtom(void)
{
DeleteAtom(atom);
}
ATOM atom;
};
/*
* Local variables
*/
static CGlobalAtom ga;
CHotKeyCtrl::CHotKeyCtrl(void)
: m_hWnd(NULL)
, m_pfnOrigCtlProc(NULL)
, controldown(false)
, shiftdown(false)
, menudown(false)
, lwindown(false)
{
}
CHotKeyCtrl::~CHotKeyCtrl(void)
{
}
BOOL CHotKeyCtrl::ConvertEditToHotKeyCtrl(HWND hwndCtl)
{
// Subclass the existing control.
m_pfnOrigCtlProc = (WNDPROC)GetWindowLongPtr(hwndCtl, GWLP_WNDPROC);
SetProp(hwndCtl, PROP_OBJECT_PTR, (HANDLE)this);
SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, (LONG_PTR)(WNDPROC)_HotKeyProc);
kb_hook = SetWindowsHookEx(WH_KEYBOARD, _KeyboardProc, NULL, GetCurrentThreadId());
m_hWnd = hwndCtl;
return TRUE;
}
BOOL CHotKeyCtrl::ConvertEditToHotKeyCtrl(HWND hwndParent, UINT uiCtlId)
{
return ConvertEditToHotKeyCtrl(GetDlgItem(hwndParent, uiCtlId));
}
void CHotKeyCtrl::SetHKText(WORD hk)
{
wchar_t buf[MAX_PATH] = {0};
wchar_t result[MAX_PATH] = {0};
BYTE h = hk >> 8;
if (h & HOTKEYF_CONTROL)
{
LONG sc = MapVirtualKey(VK_CONTROL, MAPVK_VK_TO_VSC);
sc <<= 16;
GetKeyNameText(sc, buf, _countof(buf));
wcscat_s(result, _countof(result), buf);
}
if (h & HOTKEYF_SHIFT)
{
if (result[0])
wcscat_s(result, _countof(result), L" + ");
LONG sc = MapVirtualKey(VK_SHIFT, MAPVK_VK_TO_VSC);
sc <<= 16;
GetKeyNameText(sc, buf, _countof(buf));
wcscat_s(result, _countof(result), buf);
}
if (h & HOTKEYF_ALT)
{
if (result[0])
wcscat_s(result, _countof(result), L" + ");
LONG sc = MapVirtualKey(VK_MENU, MAPVK_VK_TO_VSC);
sc <<= 16;
GetKeyNameText(sc, buf, _countof(buf));
wcscat_s(result, _countof(result), buf);
}
if (h & HOTKEYF_EXT)
{
if (result[0])
wcscat_s(result, _countof(result), L" + ");
LONG sc = MapVirtualKey(VK_LWIN, MAPVK_VK_TO_VSC);
sc |= 0x0100;
sc <<= 16;
GetKeyNameText(sc, buf, _countof(buf));
wcscat_s(result, _countof(result), buf);
}
if (result[0])
wcscat_s(result, _countof(result), L" + ");
LONG nScanCode = MapVirtualKey(LOBYTE(hk), MAPVK_VK_TO_VSC);
switch (LOBYTE(hk))
{
// Keys which are "extended" (except for Return which is Numeric Enter as extended)
case VK_INSERT:
case VK_DELETE:
case VK_HOME:
case VK_END:
case VK_NEXT: // Page down
case VK_PRIOR: // Page up
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_SNAPSHOT:
nScanCode |= 0x0100; // Add extended bit
}
nScanCode <<= 16;
GetKeyNameText(nScanCode, buf, _countof(buf));
wcscat_s(result, _countof(result), buf);
::SetWindowText(m_hWnd, result);
}
LRESULT CALLBACK CHotKeyCtrl::_KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
HWND hWndFocus = ::GetFocus();
if (hWndFocus)
{
CHotKeyCtrl *pHyperLink = (CHotKeyCtrl *)GetProp(hWndFocus, PROP_OBJECT_PTR);
if ((pHyperLink) && (pHyperLink->m_hWnd == hWndFocus))
{
if (lParam & 0x80000000)
{
// WM_KEYUP
if (wParam == VK_CONTROL)
{
pHyperLink->controldown = false;
}
else if (wParam == VK_SHIFT)
{
pHyperLink->shiftdown = false;
}
else if (wParam == VK_MENU)
{
pHyperLink->menudown = false;
}
else if (wParam == VK_LWIN)
{
pHyperLink->lwindown = false;
}
else
{
WORD hk = 0;
if (pHyperLink->controldown)
{
hk |= HOTKEYF_CONTROL;
}
if (pHyperLink->shiftdown)
{
hk |= HOTKEYF_SHIFT;
}
if (pHyperLink->menudown)
{
hk |= HOTKEYF_ALT;
}
if (pHyperLink->lwindown)
{
hk |= HOTKEYF_EXT;
}
pHyperLink->hotkey = MAKEWORD(wParam, hk);
pHyperLink->SetHKText(pHyperLink->hotkey);
return 1; //we processed it
}
}
else
{
// WM_KEYDOWN
if (wParam == VK_CONTROL)
{
pHyperLink->controldown = true;
}
else if (wParam == VK_SHIFT)
{
pHyperLink->shiftdown = true;
}
else if (wParam == VK_MENU)
{
pHyperLink->menudown = true;
}
else if (wParam == VK_LWIN)
{
pHyperLink->lwindown = true;
}
else
{
WORD hk = 0;
if (pHyperLink->controldown)
{
hk |= HOTKEYF_CONTROL;
}
if (pHyperLink->shiftdown)
{
hk |= HOTKEYF_SHIFT;
}
if (pHyperLink->menudown)
{
hk |= HOTKEYF_ALT;
}
if (pHyperLink->lwindown)
{
hk |= HOTKEYF_EXT;
}
pHyperLink->hotkey = MAKEWORD(wParam, hk);
pHyperLink->SetHKText(pHyperLink->hotkey);
return 1; //we processed it
}
}
return CallNextHookEx(pHyperLink->kb_hook, code, wParam, lParam);
}
}
return 0;
}
LRESULT CALLBACK CHotKeyCtrl::_HotKeyProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
CHotKeyCtrl *pHyperLink = (CHotKeyCtrl *)GetProp(hwnd, PROP_OBJECT_PTR);
switch (message)
{
case WM_SHOWWINDOW:
pHyperLink->SetHKText(pHyperLink->hotkey);
break;
case WM_GETDLGCODE:
return DLGC_WANTALLKEYS;
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
{
return 0;
}
case WM_SYSKEYUP:
case WM_KEYUP:
{
return 0;
}
case WM_CHAR:
return 0;
case WM_DESTROY:
{
SetWindowLongPtr(hwnd, GWLP_WNDPROC,
(LONG_PTR)pHyperLink->m_pfnOrigCtlProc);
RemoveProp(hwnd, PROP_OBJECT_PTR);
break;
}
}
return CallWindowProc(pHyperLink->m_pfnOrigCtlProc, hwnd, message,
wParam, lParam);
}
void CHotKeyCtrl::SetHotKey(WPARAM hk)
{
hotkey = (WORD)hk;
SetHKText((WORD)hk);
}
WPARAM CHotKeyCtrl::GetHotKey()
{
return hotkey;
}