Repository navigation
Expand file tree
/
Copy pathHeuristicsInput.cpp
More file actions
360 lines (299 loc) · 7.89 KB
/
Copy pathHeuristicsInput.cpp
File metadata and controls
360 lines (299 loc) · 7.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
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
#include "HeuristicsInput.h"
#include <string>
#include <utility>
namespace
{
constexpr UINT kToUnicodeNoStateChange = 1u << 2;
constexpr int kToUnicodeBufferLength = 16;
CommandPtr CombineWithReset(CommandPtr resetCommand, CommandPtr command)
{
if (!resetCommand)
{
return command;
}
if (!command || !command->ShouldLog())
{
return resetCommand;
}
auto batch = std::make_unique<CommandBatch>(std::wstring(command->Reason()));
batch->Add(std::move(resetCommand));
batch->Add(std::move(command));
return batch;
}
}
HeuristicsInput::HeuristicsInput()
{
GetKeyboardState(m_KeyState.data());
m_Focus.Hwnd = GetForegroundWindow();
}
CommandPtr HeuristicsInput::ProcessKeyboardEvent(WPARAM message, const KBDLLHOOKSTRUCT& keyboard)
{
const bool keyDown =
message == WM_KEYDOWN ||
message == WM_SYSKEYDOWN;
const bool keyUp =
message == WM_KEYUP ||
message == WM_SYSKEYUP;
if (!keyDown && !keyUp)
{
return CommandUtils::MakeNoOpCommand();
}
if ((keyboard.flags & LLKHF_INJECTED) != 0)
{
return CommandUtils::MakeNoOpCommand();
}
std::lock_guard<std::mutex> lock(m_Mutex);
const DWORD normalizedKey = NormalizeModifierKey(keyboard.vkCode, keyboard);
UpdateKeyState(normalizedKey, keyDown);
if (!keyDown)
{
return CommandUtils::MakeNoOpCommand();
}
CommandPtr resetCommand;
if (SyncForegroundWindowUnlocked())
{
resetCommand = std::make_unique<ClearCommand>(L"foreground");
}
return CombineWithReset(std::move(resetCommand), BuildKeyboardCommand(keyboard));
}
CommandPtr HeuristicsInput::ProcessMouseEvent(WPARAM message)
{
switch (message)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
return std::make_unique<ClearCommand>(L"mouse");
default:
return CommandUtils::MakeNoOpCommand();
}
}
CommandPtr HeuristicsInput::ProcessWindowEvent(DWORD event, HWND hwnd, LONG idObject, LONG idChild)
{
if (!hwnd)
{
return CommandUtils::MakeNoOpCommand();
}
if (event != EVENT_SYSTEM_FOREGROUND && event != EVENT_OBJECT_FOCUS)
{
return CommandUtils::MakeNoOpCommand();
}
std::lock_guard<std::mutex> lock(m_Mutex);
const bool changed =
m_Focus.Hwnd != hwnd ||
m_Focus.IdObject != idObject ||
m_Focus.IdChild != idChild;
if (!changed)
{
return CommandUtils::MakeNoOpCommand();
}
m_Focus.Hwnd = hwnd;
m_Focus.IdObject = idObject;
m_Focus.IdChild = idChild;
if (event == EVENT_SYSTEM_FOREGROUND)
{
return std::make_unique<ClearCommand>(L"foreground-event");
}
return std::make_unique<ClearCommand>(L"focus-event");
}
bool HeuristicsInput::IsDown(int virtualKey) const
{
if (virtualKey < 0 || virtualKey > 255)
{
return false;
}
return (m_KeyState[static_cast<std::size_t>(virtualKey)] & 0x80) != 0;
}
bool HeuristicsInput::IsWinKeyDown() const
{
return IsDown(VK_LWIN) || IsDown(VK_RWIN);
}
bool HeuristicsInput::IsShortcutModifierActive() const
{
const bool ctrl = IsDown(VK_CONTROL);
const bool alt = IsDown(VK_MENU);
const bool rightAlt = IsDown(VK_RMENU);
const bool altGr = ctrl && rightAlt;
if (IsWinKeyDown())
{
return true;
}
if (ctrl && !altGr)
{
return true;
}
if (alt && !altGr)
{
return true;
}
return false;
}
bool HeuristicsInput::SyncForegroundWindowUnlocked()
{
HWND hwnd = GetForegroundWindow();
if (hwnd == m_Focus.Hwnd)
{
return false;
}
m_Focus.Hwnd = hwnd;
m_Focus.IdObject = 0;
m_Focus.IdChild = 0;
return true;
}
bool HeuristicsInput::IsNavigationOrCaretChangingKey(DWORD virtualKey) const
{
switch (virtualKey)
{
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
case VK_HOME:
case VK_END:
case VK_PRIOR:
case VK_NEXT:
case VK_INSERT:
case VK_DELETE:
return true;
default:
return false;
}
}
bool HeuristicsInput::IsImeKey(DWORD virtualKey) const
{
switch (virtualKey)
{
case VK_PROCESSKEY:
case VK_CONVERT:
case VK_NONCONVERT:
case VK_ACCEPT:
case VK_MODECHANGE:
case VK_KANA:
case VK_HANJA:
case VK_IME_ON:
case VK_IME_OFF:
return true;
default:
return false;
}
}
bool HeuristicsInput::IsModifierOnlyKey(DWORD virtualKey) const
{
switch (virtualKey)
{
case VK_SHIFT:
case VK_LSHIFT:
case VK_RSHIFT:
case VK_CONTROL:
case VK_LCONTROL:
case VK_RCONTROL:
case VK_MENU:
case VK_LMENU:
case VK_RMENU:
case VK_LWIN:
case VK_RWIN:
return true;
default:
return false;
}
}
DWORD HeuristicsInput::NormalizeModifierKey(DWORD virtualKey, const KBDLLHOOKSTRUCT& keyboard) const
{
switch (virtualKey)
{
case VK_SHIFT:
{
const UINT mappedKey = MapVirtualKeyW(keyboard.scanCode, MAPVK_VSC_TO_VK_EX);
if (mappedKey == VK_LSHIFT || mappedKey == VK_RSHIFT)
{
return mappedKey;
}
return virtualKey;
}
case VK_CONTROL:
return (keyboard.flags & LLKHF_EXTENDED) != 0 ? VK_RCONTROL : VK_LCONTROL;
case VK_MENU:
return (keyboard.flags & LLKHF_EXTENDED) != 0 ? VK_RMENU : VK_LMENU;
default:
return virtualKey;
}
}
void HeuristicsInput::UpdateKeyState(DWORD virtualKey, bool down)
{
if (virtualKey <= 255)
{
BYTE& state = m_KeyState[static_cast<std::size_t>(virtualKey)];
state = static_cast<BYTE>((state & 0x01) | (down ? 0x80 : 0x00));
}
RecomputeAggregateModifier(VK_SHIFT, VK_LSHIFT, VK_RSHIFT);
RecomputeAggregateModifier(VK_CONTROL, VK_LCONTROL, VK_RCONTROL);
RecomputeAggregateModifier(VK_MENU, VK_LMENU, VK_RMENU);
}
void HeuristicsInput::RecomputeAggregateModifier(int genericKey, int leftKey, int rightKey)
{
m_KeyState[static_cast<std::size_t>(genericKey)] =
IsDown(leftKey) || IsDown(rightKey) ? 0x80 : 0x00;
}
CommandPtr HeuristicsInput::BuildKeyboardCommand(const KBDLLHOOKSTRUCT& keyboard)
{
const DWORD virtualKey = keyboard.vkCode;
if (IsShortcutModifierActive() && !IsModifierOnlyKey(virtualKey))
{
return std::make_unique<ClearCommand>(L"shortcut");
}
if (virtualKey == VK_BACK)
{
return std::make_unique<DeleteLastCommand>(L"backspace");
}
if (virtualKey == VK_RETURN)
{
return std::make_unique<AppendTextCommand>(std::wstring(1, L'\n'), L"enter");
}
if (virtualKey == VK_ESCAPE)
{
return std::make_unique<ClearCommand>(L"escape");
}
if (IsImeKey(virtualKey))
{
return std::make_unique<ClearCommand>(L"ime");
}
if (IsNavigationOrCaretChangingKey(virtualKey))
{
return std::make_unique<ClearCommand>(L"navigation");
}
wchar_t out[kToUnicodeBufferLength] = {};
UINT scanCode = keyboard.scanCode;
if ((keyboard.flags & LLKHF_EXTENDED) != 0)
{
scanCode |= 0x0100;
}
int result = ToUnicodeEx(
static_cast<UINT>(virtualKey),
scanCode,
m_KeyState.data(),
out,
kToUnicodeBufferLength,
kToUnicodeNoStateChange,
GetForegroundKeyboardLayout()
);
if (result <= 0)
{
return CommandUtils::MakeNoOpCommand();
}
if (result > kToUnicodeBufferLength)
{
result = kToUnicodeBufferLength;
}
return std::make_unique<AppendTextCommand>(std::wstring(out, out + result), L"char");
}
HKL HeuristicsInput::GetForegroundKeyboardLayout() const
{
HWND hwnd = GetForegroundWindow();
if (!hwnd)
{
return GetKeyboardLayout(GetCurrentThreadId());
}
DWORD threadId = GetWindowThreadProcessId(hwnd, nullptr);
return GetKeyboardLayout(threadId);
}