-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistryKeyRemapping.cpp
More file actions
208 lines (202 loc) · 4.96 KB
/
RegistryKeyRemapping.cpp
File metadata and controls
208 lines (202 loc) · 4.96 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
#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;
};
int ReadKeyBindings(KeyBindingsEntry arr[], int maxArrCount)
{
const DWORD maxBufferSize = 8192;
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader) - sizeof(DWORD)) / sizeof(KeyBindingsEntry);
BYTE buffer[maxBufferSize];
DWORD bufferSize = maxBufferSize;
DWORD type = 0;
LSTATUS status = RegGetValueA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", "Scancode Map", RRF_RT_REG_BINARY, &type, &buffer[0], &bufferSize);
if (status == ERROR_FILE_NOT_FOUND)
{
return 0;
}
else if (status == ERROR_UNSUPPORTED_TYPE)
{
return 0;
}
else if (status != ERROR_SUCCESS)
{
return 0;
}
if (bufferSize < 20)
{
return 0;
}
KeyBindingsHeader* pHeader = (KeyBindingsHeader*)buffer;
//using volatile to prevent importing memcpy
KeyBindingsEntry* volatile pEntry = (KeyBindingsEntry*volatile)(buffer + sizeof(KeyBindingsHeader));
int count = pHeader->numberOfEntries - 1;
if (count > maxArrCount)
{
count = maxArrCount;
}
if (count < 0 || count > maxCount ||
count * sizeof(KeyBindingsEntry) + sizeof(KeyBindingsHeader) + sizeof(DWORD) < bufferSize)
{
return 0;
}
DWORD* pTerminator = (DWORD*)(buffer + sizeof(KeyBindingsHeader) + count * sizeof(KeyBindingsEntry));
if (*pTerminator != 0)
{
return 0;
}
for (int i = 0; i < count; i++)
{
arr[i] = *pEntry;
pEntry++;
}
return count;
}
bool WriteKeyBindings(KeyBindingsEntry arr[], int count)
{
const DWORD maxBufferSize = 8192;
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader) - sizeof(DWORD)) / sizeof(KeyBindingsEntry);
if (count < 0 || count > maxCount)
{
return false;
}
bool result = false;
HKEY key = NULL;
LSTATUS status;
if (count == 0)
{
//Delete all key bindings when count is 0
status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", 0, KEY_ALL_ACCESS, &key);
if (status != ERROR_SUCCESS)
{
result = true;
goto done;
}
DWORD type = 0;
status = RegGetValueA(key, NULL, "Scancode Map", RRF_RT_ANY, &type, NULL, NULL);
if (status == ERROR_SUCCESS)
{
status = RegDeleteValueA(key, "Scancode Map");
if (status == ERROR_SUCCESS)
{
result = true;
goto done;
}
}
else if (status == ERROR_FILE_NOT_FOUND)
{
result = true;
goto done;
}
goto done;
}
else
{
//Write a Key Bindings object to the registry
BYTE buffer[maxBufferSize];
BYTE* p = &buffer[0];
KeyBindingsHeader* header = (KeyBindingsHeader*)p;
header->version = 0;
header->headerFlags = 0;
header->numberOfEntries = count + 1;
p += sizeof(KeyBindingsHeader);
//using volatile to prevent importing memcpy
KeyBindingsEntry*volatile pEntry = (KeyBindingsEntry*volatile)p;
for (int i = 0; i < count; i++)
{
*pEntry = arr[i];
pEntry++;
}
p = (BYTE*)pEntry;
DWORD* p2 = (DWORD*)p;
*p2 = 0;
p += sizeof(DWORD);
int contentSize = p - buffer;
status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", 0, KEY_ALL_ACCESS, &key);
if (status == ERROR_ACCESS_DENIED)
{
goto done;
}
if (status == ERROR_FILE_NOT_FOUND)
{
status = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout", 0, NULL, NULL, KEY_ALL_ACCESS, NULL, &key, NULL);
}
if (status != ERROR_SUCCESS)
{
goto done;
}
status = RegSetValueExA(key, "Scancode Map", 0, REG_BINARY, &buffer[0], contentSize);
if (status == ERROR_SUCCESS)
{
result = true;
goto done;
}
goto done;
}
done:
if (key != NULL)
{
RegCloseKey(key);
key = NULL;
}
return result;
}
bool RegisterRemappedKey(WORD scancodeToRemap, WORD scancodeToChangeTo)
{
const DWORD maxBufferSize = 8192;
const DWORD maxCount = (maxBufferSize - sizeof(KeyBindingsHeader) - sizeof(DWORD)) / sizeof(KeyBindingsEntry);
if (scancodeToRemap == 0)
{
return false;
}
KeyBindingsEntry arr[maxCount];
int count = ReadKeyBindings(arr, maxCount);
//check for scancodeToRemap, if it exists replace it, otherwise add it to the list
bool found = false;
bool changed = false;
for (int i = 0; i < count; i++)
{
if (arr[i].sourceKey == scancodeToRemap)
{
found = true;
if (arr[i].destinationKey != scancodeToChangeTo)
{
if (scancodeToChangeTo == scancodeToRemap)
{
//swap with last element, remove last element
KeyBindingsEntry t = arr[i];
arr[i] = arr[count - 1];
arr[count - 1] = t;
count--;
}
else
{
//change last key
arr[i].destinationKey = scancodeToChangeTo;
}
changed = true;
}
break;
}
}
if (!found && count < maxCount && scancodeToChangeTo != scancodeToRemap)
{
arr[count].sourceKey = scancodeToRemap;
arr[count].destinationKey = scancodeToChangeTo;
count++;
changed = true;
}
if (changed)
{
return WriteKeyBindings(arr, count);
}
else
{
return true;
}
}