-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotkeymanager.cpp
More file actions
171 lines (148 loc) · 5.89 KB
/
hotkeymanager.cpp
File metadata and controls
171 lines (148 loc) · 5.89 KB
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
#include "hotkeymanager.h"
#include <QMetaObject>
HHOOK HotkeyManager::s_hook = nullptr;
HotkeyManager *HotkeyManager::s_instance = nullptr;
HotkeyManager::HotkeyManager(QObject *parent)
: QObject(parent) {
s_instance = this;
}
HotkeyManager::~HotkeyManager() {
if (s_hook)
UnhookWindowsHookEx(s_hook);
UnregisterHotKey(nullptr, id);
s_instance = nullptr;
}
void HotkeyManager::setEnabled(bool enabledState) {
enabled = enabledState;
}
void HotkeyManager::setEouEnabled(bool enabledState) {
eouEnabled = enabledState;
if (!eouEnabled)
eouActive = false;
}
bool HotkeyManager::setEouSequence(const QString &sequence) {
UINT mods = 0;
UINT key = 0;
if (!parseSequence(sequence, mods, key))
return false;
eouModifiers = mods;
eouVk = key;
eouActive = false;
return true;
}
bool HotkeyManager::parseSequence(const QString &sequence,
UINT &modifiers,
UINT &vk) const {
modifiers = 0;
vk = 0;
const auto parts = sequence.split('+', Qt::SkipEmptyParts);
if (parts.isEmpty())
return false;
for (const QString &raw: parts) {
const QString part = raw.trimmed();
if (part.compare("Ctrl", Qt::CaseInsensitive) == 0) modifiers |= MOD_CONTROL;
else if (part.compare("Alt", Qt::CaseInsensitive) == 0) modifiers |= MOD_ALT;
else if (part.compare("Shift", Qt::CaseInsensitive) == 0) modifiers |= MOD_SHIFT;
else if (part.compare("Win", Qt::CaseInsensitive) == 0 ||
part.compare("Meta", Qt::CaseInsensitive) == 0)
modifiers |= MOD_WIN;
else {
if (part.length() == 1) {
// single character
const wchar_t ch = part.at(0).toUpper().unicode();
const SHORT res = VkKeyScanW(ch);
if (res == -1) return false;
vk = LOBYTE(res);
} else if (part.startsWith('F', Qt::CaseInsensitive)) {
// function keys F1–F24
bool ok = false;
const int n = part.mid(1).toInt(&ok);
if (!ok || n < 1 || n > 24) return false;
vk = VK_F1 + n - 1;
} else if (part.compare("Tab", Qt::CaseInsensitive) == 0) vk = VK_TAB;
else if (part.compare("Space", Qt::CaseInsensitive) == 0) vk = VK_SPACE;
else if (part.compare("Enter", Qt::CaseInsensitive) == 0) vk = VK_RETURN;
else if (part.compare("Esc", Qt::CaseInsensitive) == 0 ||
part.compare("Escape", Qt::CaseInsensitive) == 0)
vk = VK_ESCAPE;
else return false;
}
}
return vk != 0;
}
bool HotkeyManager::registerHotkey(const QString &sequence) {
UINT mods = 0;
UINT key = 0;
if (!parseSequence(sequence, mods, key))
return false;
currentModifiers = mods;
currentVk = key;
hotkeyActive = false;
// do not use RegisterHotKey: we need full interception
UnregisterHotKey(nullptr, id);
if (!s_hook) {
s_hook = SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelProc,
GetModuleHandleW(nullptr), 0);
}
return s_hook != nullptr;
}
bool HotkeyManager::nativeEventFilter(const QByteArray &,
void *,
qintptr *) {
// WM_HOTKEY is not used — hook works at a lower level
return false;
}
LRESULT CALLBACK HotkeyManager::LowLevelProc(int nCode,
WPARAM wParam,
LPARAM lParam) {
if (nCode == HC_ACTION && s_instance && s_instance->enabled) {
const auto *kb = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
const UINT vk = kb->vkCode;
const bool ctrl = (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0;
const bool alt = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0;
const bool shift = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
const bool win = (GetAsyncKeyState(VK_LWIN) & 0x8000) ||
(GetAsyncKeyState(VK_RWIN) & 0x8000);
UINT mods = 0;
if (ctrl) mods |= MOD_CONTROL;
if (alt) mods |= MOD_ALT;
if (shift) mods |= MOD_SHIFT;
if (win) mods |= MOD_WIN;
if (mods == s_instance->currentModifiers &&
vk == s_instance->currentVk) {
// hotkey matched — emit signal and block further processing
if (!s_instance->hotkeyActive) {
s_instance->hotkeyActive = true;
QMetaObject::invokeMethod(s_instance, "hotkeyPressed",
Qt::QueuedConnection);
}
return 1;
}
if (s_instance->eouEnabled && mods == s_instance->eouModifiers &&
vk == s_instance->eouVk) {
if (!s_instance->eouActive) {
s_instance->eouActive = true;
QMetaObject::invokeMethod(s_instance, "eouPressed",
Qt::QueuedConnection);
}
return 1;
}
} else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
// Reset flag on key release to allow next press
if (s_instance->hotkeyActive) {
const UINT vk = kb->vkCode;
if (vk == s_instance->currentVk) {
s_instance->hotkeyActive = false;
}
}
if (s_instance->eouActive) {
const UINT vk = kb->vkCode;
if (vk == s_instance->eouVk) {
s_instance->eouActive = false;
}
}
}
}
return CallNextHookEx(nullptr, nCode, wParam, lParam);
}