Skip to content

Instantly share code, notes, and snippets.

@nd
Created May 5, 2020 05:37
Show Gist options
  • Save nd/f1f6af32943c288a07769ac4f017d3ad to your computer and use it in GitHub Desktop.
Save nd/f1f6af32943c288a07769ac4f017d3ad to your computer and use it in GitHub Desktop.
Disable alt-space menu on windows
// disables alt-space for the whole system. To turn it off, kill its process.
// copy to C:\Users\<User>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup to run on startup
// to compile with visual studio:
// run
// "c:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/Build/vcvarsall.bat" x64
// cl hookMainMini.cpp user32.lib
#include <windows.h>
LRESULT CALLBACK LowLevelKeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
if (code < 0) {
return CallNextHookEx(0, code, wParam, lParam);
}
KBDLLHOOKSTRUCT *info = (KBDLLHOOKSTRUCT *) lParam;
if (wParam == WM_SYSKEYDOWN && info->vkCode == VK_SPACE) {
HWND foregroundWindow = GetForegroundWindow();
if (foregroundWindow) {
// looks like we cannot fill lparam completly, e.g. we don't get repeatCount,
// at least set the altdown bit.
LPARAM appLParam = KF_ALTDOWN;
if (!PostMessageA(foregroundWindow, WM_SYSKEYDOWN, VK_SPACE, appLParam) == 0) {
return 1;
}
}
}
return CallNextHookEx(0, code, wParam, lParam);
}
INT WinMain(HINSTANCE application, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
WNDCLASS windowClass = {};
windowClass.lpfnWndProc = DefWindowProc;
windowClass.hInstance = application;
windowClass.lpszClassName = "Hooks";
RegisterClassA(&windowClass);
HWND window = CreateWindowExA(0,
windowClass.lpszClassName,
"Hooks",
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_MESSAGE, // hidden window which only dispatches messages
0,
application,
0);
if (!window) {
return 1;
}
HHOOK llkbHook = SetWindowsHookExA(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);
if (!llkbHook) {
return 1;
}
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
cleanup:
if (llkbHook) {
UnhookWindowsHookEx(llkbHook);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment