-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterceptKeys.cpp
More file actions
200 lines (171 loc) · 5.68 KB
/
InterceptKeys.cpp
File metadata and controls
200 lines (171 loc) · 5.68 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
#include "common.h"
#include "interception.h"
#include "keys.h"
#include "keymap.hpp"
#include "Service.hpp"
#include "Cli.hpp"
#include "utils.hpp"
#include <sstream>
#include <string>
int InterceptKeys() {
InterceptionContext context = interception_create_context();
InterceptionDevice device;
interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_ALL);
// Used in detectKeysOnly mode
bool detectKeysOnly = false;
size_t noOfClicks = 0;
if (gCli.detect_keys_only_flag) {
detectKeysOnly = true;
}
KeyMapEntry::removeEmptyEntries(g_MapEntries);
if (g_MapEntries.empty()) {
INTERCEPT_LOGD_N_OUT(gLogger, "No key mappings found!");
goto cleanup;
}
utils::process::raise_process_priority();
while (g_ServiceStatus.dwCurrentState != SERVICE_STOP_PENDING) {
InterceptionStroke stroke;
INTERCEPT_LOG_DEBUG(gLogger, "Waiting on context...");
device = interception_wait(context);
if (interception_is_keyboard(device)) {
INTERCEPT_LOG_DEBUG(gLogger, "Waiting for key stroke...");
interception_receive(context, device, &stroke, 1);
InterceptionKeyStroke* kstroke = (InterceptionKeyStroke*)&stroke;
int code = kstroke->code;
int state = kstroke->state;
bool is_down = (state & INTERCEPTION_KEY_UP) == 0;
bool e0 = (state & INTERCEPTION_KEY_E0) != 0; // Extended key flag
if (detectKeysOnly) {
std::stringstream _scanCode;
std::string scanCode;
int codeWithFlags = code;
if (e0) {
codeWithFlags |= (0xE0 << 8);
}
_scanCode << "0x" << std::hex << std::uppercase << codeWithFlags;
_scanCode.flags(std::ios::fmtflags());
scanCode = _scanCode.str();
// Only on click as to not cause confusion
if (is_down) {
INTERCEPT_LOGD_N_OUT(gLogger, "{}. Pressed key: {} ({}), click: {}, state: {}", ++noOfClicks, code, scanCode, is_down, state);
}
interception_send(context, device, &stroke, 1);
continue;
}
// Iterating entries to find the one that matches
// If the entry is found, send the mapped key stroke
bool sendMappedKeyStroke = false;
for (auto& entry : g_MapEntries) {
bool foundModifierKey = false;
bool foundRegularKey = false;
int fromKeyIndex = 0;
for (int fromKey : entry.from) {
int e0bits = fromKey & SC_E0;
bool extended = e0bits == SC_E0;
if (extended)
fromKey &= ~SC_E0;
INTERCEPT_LOGD_N_OUT(gLogger, "From key[{}]: {}, extended: {}, finding key: {}, e0: {}",
fromKeyIndex, fromKey, extended, code, e0);
bool isLast = fromKeyIndex == entry.from.size() - 1;
// Regular key
if (isLast) {
foundRegularKey = fromKey == code && extended == e0;
INTERCEPT_LOGD_N_OUT(gLogger, "Reached last key! found regular key? {}, ctr: {}, entry.from.size() - 1: {}",
foundRegularKey, entry.ctr(), entry.from.size() - 1);
if (foundRegularKey && entry.ctr() == entry.from.size() - 1) {
// This key should be suppressed
// Release the modifier keys
// Send the mapped key combo
sendMappedKeyStroke = true;
INTERCEPT_LOGD_N_OUT(gLogger, "Pressed regular key: {}, click: {}, state: {}",
code, is_down, state);
}
}
// Modifier combo sequence can be random
else if (fromKey == code && extended == e0) {
foundModifierKey = true;
if (is_down)
entry.clickKey(code, fromKeyIndex);
else
entry.releaseKey(code, fromKeyIndex);
INTERCEPT_LOGD_N_OUT(gLogger, "Pressed modifier key: {}, click: {}, state: {}",
code, is_down, state);
}
if (foundModifierKey || foundRegularKey)
break;
fromKeyIndex++;
}
if (sendMappedKeyStroke) {
// Release until regular key
int fromKeyIndex = 0;
InterceptionKeyStroke mkfstroke;
for (int fromKey : entry.from) {
if (fromKeyIndex == entry.from.size() - 1)
break;
int e0bits = fromKey & SC_E0;
bool extended = e0bits == SC_E0;
if (extended)
fromKey &= ~SC_E0;
mkfstroke.code = fromKey;
mkfstroke.state = INTERCEPTION_KEY_UP;
if (extended)
mkfstroke.state |= INTERCEPTION_KEY_E0;
interception_send(context, device, (InterceptionStroke*)&mkfstroke, 1);
fromKeyIndex++;
}
// Send mapped combo
InterceptionKeyStroke mktstroke;
auto sendToKey = [&](int toKey) {
int e0bits = toKey & SC_E0;
bool extended = e0bits == SC_E0;
if (extended)
toKey &= ~SC_E0;
mktstroke.code = toKey;
if (is_down)
mktstroke.state = INTERCEPTION_KEY_DOWN;
else
mktstroke.state = INTERCEPTION_KEY_UP;
if (extended)
mktstroke.state |= INTERCEPTION_KEY_E0;
interception_send(context, device, (InterceptionStroke*)&mktstroke, 1);
INTERCEPT_LOGD_N_OUT(gLogger, "Sending key: {}, click: {}, state: {}",
mktstroke.code, is_down, mktstroke.state);
};
// Send down for all keys in exact order
if (is_down) {
// All keys are pressed once
// Now only press the regular last key
if (entry.allToKeysDown) {
sendToKey(entry.to.back());
}
else {
entry.allToKeysDown = true;
for (auto toKey : entry.to) {
sendToKey(toKey);
}
}
}
// Send up for all keys in reverse order
else {
entry.allToKeysDown = false;
for (auto toKeyIt = entry.to.rbegin(); toKeyIt != entry.to.rend();
++toKeyIt) {
sendToKey(*toKeyIt);
}
}
if (entry.to.empty()) {
INTERCEPT_LOGD_N_OUT(gLogger, "Sending key: disabled");
}
break;
};
}
if (sendMappedKeyStroke)
continue;
}
// Default: pass through
interception_send(context, device, &stroke, 1);
}
cleanup:
interception_destroy_context(context);
return 0;
}