-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
190 lines (164 loc) · 6.35 KB
/
Source.cpp
File metadata and controls
190 lines (164 loc) · 6.35 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
// hyperkey_squatter.cpp
// Build: cl /EHsc /W4 /O2 /std:c++17 hyperkey_squatter.cpp /link user32.lib
//
// Usage: hyperkey_squatter.exe [--allwin]
//
// Default: squat Hyper (Win+Ctrl+Alt+Shift) + {W T Y O P D L X N Space} while
// Explorer reinitializes, then release so a userland daemon (AutoHotkey, etc.)
// can bind them. Stays alive afterwards so nssm doesn't restart us in a loop.
//
// --allwin: additionally register plain Win+<same key set> and HOLD those for
// the entire process lifetime — they are never released, so Explorer / Office
// hotkeys on those keys stay dead until the service is stopped.
//
// Graceful shutdown on CTRL_C / CTRL_CLOSE / service stop.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <array>
#include <chrono>
#include <cstdio>
#include <cstring>
namespace {
constexpr UINT kHyperMods = MOD_WIN | MOD_CONTROL | MOD_ALT | MOD_SHIFT | MOD_NOREPEAT;
constexpr UINT kWinMods = MOD_WIN | MOD_NOREPEAT;
constexpr std::array<UINT, 10> kKeys = {
'W', 'T', 'Y', 'O', 'P', 'D', 'L', 'X', 'N', VK_SPACE
};
constexpr int kHyperIdBase = 0; // ids 0..9
constexpr int kWinIdBase = 100; // ids 100..109
constexpr auto kExplorerBootTime = std::chrono::seconds(4);
HANDLE g_exitEvent = nullptr;
BOOL WINAPI CtrlHandler(DWORD type) {
switch (type) {
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
if (g_exitEvent) SetEvent(g_exitEvent);
return TRUE;
}
return FALSE;
}
bool KillExplorer() {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) return false;
PROCESSENTRY32W pe{};
pe.dwSize = sizeof(pe);
bool killedAny = false;
for (BOOL more = Process32FirstW(snap, &pe); more; more = Process32NextW(snap, &pe)) {
if (_wcsicmp(pe.szExeFile, L"explorer.exe") != 0) continue;
HANDLE p = OpenProcess(PROCESS_TERMINATE, FALSE, pe.th32ProcessID);
if (!p) continue;
if (TerminateProcess(p, 0)) killedAny = true;
CloseHandle(p);
}
CloseHandle(snap);
return killedAny;
}
bool StartExplorer() {
wchar_t winDir[MAX_PATH];
UINT n = GetSystemWindowsDirectoryW(winDir, MAX_PATH);
if (!n || n >= MAX_PATH) return false;
wchar_t cmd[MAX_PATH];
if (swprintf_s(cmd, L"\"%s\\explorer.exe\"", winDir) < 0) return false;
STARTUPINFOW si{};
si.cb = sizeof(si);
PROCESS_INFORMATION pi{};
if (!CreateProcessW(nullptr, cmd, nullptr, nullptr, FALSE,
DETACHED_PROCESS, nullptr, winDir, &si, &pi)) {
return false;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return true;
}
int RegisterSet(UINT mods, int idBase, const char* label) {
int got = 0;
for (size_t i = 0; i < kKeys.size(); ++i) {
if (RegisterHotKey(nullptr, idBase + static_cast<int>(i), mods, kKeys[i])) {
++got;
} else {
std::fprintf(stderr, "RegisterHotKey(%s vk=0x%02X) failed (%lu)\n",
label, kKeys[i], GetLastError());
}
}
return got;
}
void UnregisterSet(int idBase) {
for (size_t i = 0; i < kKeys.size(); ++i) {
UnregisterHotKey(nullptr, idBase + static_cast<int>(i));
}
}
// Keeps the thread (and therefore any still-registered hotkeys) alive until
// g_exitEvent is signaled. Drains the message queue so WM_HOTKEY doesn't pile up.
void PumpUntilExit() {
for (;;) {
DWORD r = MsgWaitForMultipleObjects(1, &g_exitEvent, FALSE, INFINITE, QS_ALLINPUT);
if (r == WAIT_OBJECT_0) return;
if (r == WAIT_OBJECT_0 + 1) {
MSG msg;
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) return;
// intentionally drop WM_HOTKEY — we squat, we don't handle
}
}
}
}
// Waitable sleep: honors CTRL-C during the 4 s boot hold.
// Returns true if the full duration elapsed, false if exit was signaled.
bool WaitableSleep(std::chrono::milliseconds ms) {
return WaitForSingleObject(g_exitEvent, static_cast<DWORD>(ms.count())) == WAIT_TIMEOUT;
}
} // namespace
int main(int argc, char* argv[]) {
bool allWin = false;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--allwin") == 0) {
allWin = true;
} else if (std::strcmp(argv[i], "--help") == 0 || std::strcmp(argv[i], "-h") == 0) {
std::fprintf(stderr,
"Usage: %s [--allwin]\n"
" --allwin Also hold plain Win+<key> for the same key set,\n"
" for the lifetime of the process.\n",
argv[0]);
return 0;
} else {
std::fprintf(stderr, "unknown arg: %s\n", argv[i]);
return 2;
}
}
g_exitEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
if (!g_exitEvent) {
std::fprintf(stderr, "CreateEvent failed (%lu)\n", GetLastError());
return 1;
}
SetConsoleCtrlHandler(CtrlHandler, TRUE);
if (!KillExplorer()) {
std::fprintf(stderr, "warn: no running explorer.exe found to kill\n");
}
int gotHyper = RegisterSet(kHyperMods, kHyperIdBase, "Hyper");
int gotWin = allWin ? RegisterSet(kWinMods, kWinIdBase, "Win") : 0;
std::fprintf(stderr, "squatted %d Hyper + %d Win combos%s\n",
gotHyper, gotWin, allWin ? " (--allwin)" : "");
if (!StartExplorer()) {
std::fprintf(stderr, "error: CreateProcess(explorer.exe) failed (%lu)\n",
GetLastError());
}
// Hold the squat while Explorer boots and tries (and fails) to register
// its own Hyper combos. CTRL-C during this window still works.
if (WaitableSleep(std::chrono::duration_cast<std::chrono::milliseconds>(kExplorerBootTime))) {
// Release Hyper slots so AHK (or whatever) can bind them.
UnregisterSet(kHyperIdBase);
std::fprintf(stderr, "released Hyper combos; Win combos %s\n",
allWin ? "still held" : "not applicable");
// Stay alive so nssm doesn't restart us. If --allwin, we're still
// holding those registrations; if not, we're just parked.
PumpUntilExit();
}
UnregisterSet(kHyperIdBase);
if (allWin) UnregisterSet(kWinIdBase);
CloseHandle(g_exitEvent);
return 0;
}