-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistryKeyRemapping.cpp
More file actions
296 lines (275 loc) · 8.16 KB
/
RegistryKeyRemapping.cpp
File metadata and controls
296 lines (275 loc) · 8.16 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
#define WIN32_LEAN_AND_MEAN 1
#define _CRT_SECURE_NO_WARNINGS 1
#include <Windows.h>
#include "RegistryKeyRemapping.h"
struct KeyBindingsHeader
{
DWORD version;
DWORD headerFlags;
DWORD numberOfEntries;
};
struct KeyBindingsEntry
{
WORD destinationKey;
WORD sourceKey;
};
static bool ReadBufferFromRegistry(DWORD buffer[], HKEY hkey, const char *keyName, const char *valueName)
{
const DWORD maxBufferSize = 8192;
const DWORD minBufferSize = sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader);
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader)) / sizeof(KeyBindingsEntry);
DWORD bufferSize = maxBufferSize;
DWORD type = 0;
DWORD* const _arr = (DWORD*)((BYTE*)buffer + sizeof(KeyBindingsHeader));
KeyBindingsHeader* const header = (KeyBindingsHeader*)buffer;
DWORD& count = header->numberOfEntries;
LSTATUS status = RegGetValueA(hkey, keyName, valueName, RRF_RT_REG_BINARY, &type, &buffer[0], &bufferSize);
if (status == ERROR_SUCCESS &&
bufferSize >= minBufferSize &&
count < maxCount &&
header->version == 0 &&
header->headerFlags == 0 &&
count * sizeof(DWORD) + sizeof(KeyBindingsHeader) <= bufferSize &&
_arr[count - 1] == 0)
{
return true;
}
else
{
memset(buffer, 0, maxBufferSize);
count = 1;
return false;
}
}
static bool ReadBufferFromRegistry(DWORD buffer[])
{
return ReadBufferFromRegistry(buffer, HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", "Scancode Map");
}
static bool DeleteRegistryKey(HKEY hkey, const char* keyName, const char *valueName)
{
HKEY key = NULL;
LSTATUS status = RegOpenKeyExA(hkey, keyName, 0, KEY_ALL_ACCESS, &key);
if (status == ERROR_SUCCESS)
{
status = RegDeleteValueA(key, valueName);
}
return status == ERROR_SUCCESS;
}
static bool DeleteRegistryKey()
{
return DeleteRegistryKey(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", "Scancode Map");
}
static bool WriteBufferToRegistry(DWORD buffer[], HKEY hkey, const char* keyName, const char* valueName, DWORD dwOptions)
{
//const DWORD maxBufferSize = 8192;
//const DWORD minBufferSize = sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader) + sizeof(DWORD);
//const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader)) / sizeof(KeyBindingsEntry);
//DWORD bufferSize = maxBufferSize;
//DWORD type = 0;
//DWORD* const _arr = (DWORD*)((BYTE*)buffer + sizeof(KeyBindingsHeader));
KeyBindingsHeader* const header = (KeyBindingsHeader*)buffer;
//KeyBindingsEntry* const arr = (KeyBindingsEntry*)_arr;
DWORD& count = header->numberOfEntries;
HKEY key = NULL;
bool result = false;
LSTATUS status = RegOpenKeyExA(hkey, keyName, 0, KEY_ALL_ACCESS, &key);
if (status == ERROR_FILE_NOT_FOUND)
{
status = RegCreateKeyExA(hkey, keyName, 0, NULL, dwOptions, KEY_ALL_ACCESS, NULL, &key, NULL);
}
if (status != ERROR_SUCCESS)
{
return false;
}
//if (count <= 1)
//{
// //Delete all key bindings when there aren't any
// status = RegDeleteValueA(key, valueName);
//}
//else
{
status = RegSetValueExA(key, valueName, 0, REG_BINARY, (const BYTE*)&buffer[0], (count + 3) * sizeof(DWORD));
}
RegCloseKey(key);
if (status == ERROR_SUCCESS)
{
result = true;
}
return result;
}
static bool WriteBufferToRegistry(DWORD buffer[])
{
return WriteBufferToRegistry(buffer, HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", "Scancode Map", 0);
}
bool RegisterRemappedKey(WORD scancodeToRemap, WORD scancodeToChangeTo)
{
const DWORD maxBufferSize = 8192;
//const DWORD minBufferSize = sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader) + sizeof(DWORD);
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader)) / sizeof(KeyBindingsEntry);
if (scancodeToRemap == 0)
{
return false;
}
DWORD buffer[maxBufferSize / sizeof(DWORD)];
DWORD* const _arr = (DWORD*)((BYTE*)buffer + sizeof(KeyBindingsHeader));
KeyBindingsHeader* const header = (KeyBindingsHeader*)buffer;
KeyBindingsEntry* const arr = (KeyBindingsEntry*)_arr;
DWORD &count = header->numberOfEntries;
//note: count includes 0 termiantor at the end
bool okay = ReadBufferFromRegistry(buffer);
//check for scancodeToRemap, if it exists, replace it, otherwise add it to the list
bool changed = false;
for (DWORD i = 0;; i++)
{
if (arr[i].sourceKey == scancodeToRemap)
{
if (arr[i].destinationKey != scancodeToChangeTo)
{
if (scancodeToChangeTo == scancodeToRemap)
{
//move last element to this slot
_arr[i] = _arr[count - 2];
_arr[count - 2] = 0;
count--;
}
else
{
//change last key
arr[i].destinationKey = scancodeToChangeTo;
}
changed = true;
}
break;
}
else if (_arr[i] == 0)
{
//we got a zero or terminator
//if we have room, assign
if (i < maxCount - 1)
{
_arr[i] = ((DWORD)scancodeToChangeTo | ((DWORD)scancodeToRemap << 16));
changed = true;
}
//if we're at the end, expand
if (i == count - 1)
{
_arr[count] = 0;
count++;
}
else
{
return false;
}
break;
}
}
if (changed)
{
if (count <= 1)
{
DeleteRegistryKey();
}
else
{
return WriteBufferToRegistry(buffer);
}
}
else
{
return true;
}
}
WORD GetRemappedKey(WORD scancodeToRemap)
{
const DWORD maxBufferSize = 8192;
const DWORD minBufferSize = sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader) + sizeof(DWORD);
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader)) / sizeof(KeyBindingsEntry);
DWORD buffer[maxBufferSize / sizeof(DWORD)];
DWORD* const _arr = (DWORD*)((BYTE*)buffer + sizeof(KeyBindingsHeader));
KeyBindingsHeader* const header = (KeyBindingsHeader*)buffer;
KeyBindingsEntry* const arr = (KeyBindingsEntry*)_arr;
DWORD& count = header->numberOfEntries;
//note: count includes 0 termiantor at the end
bool okay = ReadBufferFromRegistry(buffer);
if (!okay) return 0xFFFF;
for (DWORD i = 0;; i++)
{
if (arr[i].sourceKey == scancodeToRemap)
{
return arr[i].destinationKey;
}
else if (i == count - 1)
{
return 0xFFFF;
}
}
}
//Converts an unsigned int into a numeric string, along with a null terminator.
//Returns NULL if buffer does not have room for the whole string.
//Otherwise returns the position after the end of the number
char* my_itoa(char* str, size_t bufferSize, int position, unsigned int value)
{
char temp[12];
char* p = &temp[10];
p[1] = 0;
*p = '0';
if (value > 0)
{
while (value > 0)
{
unsigned int modulo = value % 10;
*p = '0' + modulo;
p--;
value /= 10;
}
p++;
}
size_t size2 = temp + 11 - p;
if (position < 0)
{
return NULL;
}
if (size2 + position + 1 < bufferSize)
{
memcpy(str + position, p, size2 + 1);
return str + position + size2;
}
return NULL;
}
WORD UpdateVolatile()
{
//We keep a copy of "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout\\Scancode Map"
//in the registry at "HKCU\\Volatile Environment\\<session id>\\NoCopilotKey-Scancode Map"
//so we can try to guess what the key remapping was at login time rather than what's there now.
//Because it's a volatile key, it is automatically deleted from the registry on logout/restart.
const DWORD maxBufferSize = 8192;
const DWORD minBufferSize = sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader) + sizeof(DWORD);
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader)) / sizeof(KeyBindingsEntry);
DWORD buffer[maxBufferSize / sizeof(DWORD)];
DWORD* const _arr = (DWORD*)((BYTE*)buffer + sizeof(KeyBindingsHeader));
KeyBindingsHeader* const header = (KeyBindingsHeader*)buffer;
KeyBindingsEntry* const arr = (KeyBindingsEntry*)_arr;
DWORD& count = header->numberOfEntries;
char regKeyName[MAX_PATH] = {};
strcpy(regKeyName, "Volatile Environment\\");
DWORD sessionId = 0;
ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
my_itoa(regKeyName, MAX_PATH, strlen(regKeyName), sessionId);
bool hasBeenRead = ReadBufferFromRegistry(buffer, HKEY_CURRENT_USER, regKeyName, "NoCopilotKey-Scancode Map");
if (!hasBeenRead)
{
hasBeenRead = ReadBufferFromRegistry(buffer);
WriteBufferToRegistry(buffer, HKEY_CURRENT_USER, regKeyName, "NoCopilotKey-Scancode Map", REG_OPTION_VOLATILE);
}
for (DWORD i = 0;; i++)
{
if (arr[i].sourceKey == SCANCODE_F23)
{
return arr[i].destinationKey;
}
else if (i == count - 1)
{
return 0xFFFF;
}
}
}