Created
July 8, 2015 16:28
-
-
Save urraka/3a4a336f34d7ff1c7af3 to your computer and use it in GitHub Desktop.
Keyboard hook for preventing SublimeText from focusing menu when pressing ALT on windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gcc -DHOOKDLL -shared -o bin/st-alt-hook.dll st-alt-hook.c | |
// gcc bin/st-alt-hook.dll -mwindows st-alt-hook.c -o bin/st-alt-hook.exe | |
#include <windows.h> | |
#ifdef HOOKDLL | |
// *************** DLL *************** | |
static HHOOK hKeyboardHook = 0; | |
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) | |
{ | |
if ((nCode >= 0) && (wParam == VK_MENU) && | |
(HIWORD(lParam) & KF_REPEAT) == 0 && | |
(HIWORD(lParam) & KF_UP) == 0) | |
{ | |
char classname[256]; | |
GetClassName(GetForegroundWindow(), classname, sizeof(classname)); | |
if (strcmp(classname, "PX_WINDOW_CLASS") == 0) | |
return 1; | |
} | |
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); | |
} | |
BOOL __declspec(dllexport) RegisterHook(HMODULE hLib) | |
{ | |
if (hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hLib, 0)) | |
return TRUE; | |
else | |
return FALSE; | |
} | |
void __declspec(dllexport) UnRegisterHook() | |
{ | |
if (hKeyboardHook) | |
UnhookWindowsHookEx(hKeyboardHook); | |
} | |
#else | |
// *************** EXE *************** | |
#define NAME "st-alt-hook" | |
BOOL __declspec(dllimport) RegisterHook(HMODULE); | |
void __declspec(dllimport) UnRegisterHook(); | |
LRESULT CALLBACK HookWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); | |
static HMODULE hLib = 0; | |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) | |
{ | |
WNDCLASS wc; | |
MSG msg; | |
HWND hWnd = FindWindow(NAME, NAME); | |
if (hWnd) | |
{ | |
if (strstr(szCmdLine, "--exit")) | |
SendMessage(hWnd, WM_CLOSE, 0, 0); | |
else | |
MessageBox(NULL, NAME " is already running.", NAME, MB_OK | MB_ICONINFORMATION); | |
return 0; | |
} | |
if (!(hLib = LoadLibrary(NAME ".dll"))) | |
{ | |
MessageBox(NULL, "Error loading " NAME ".dll.", NAME, MB_OK | MB_ICONERROR); | |
return 0; | |
} | |
if (!RegisterHook(hLib)) | |
{ | |
MessageBox(NULL, "Error setting hook procedure.", NAME, MB_OK | MB_ICONERROR); | |
return 0; | |
} | |
wc.style = 0; | |
wc.lpfnWndProc = HookWndProc; | |
wc.cbClsExtra = 0; | |
wc.cbWndExtra = 0; | |
wc.hInstance = hInstance; | |
wc.hIcon = NULL; | |
wc.hCursor = NULL; | |
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | |
wc.lpszMenuName = NULL; | |
wc.lpszClassName = NAME; | |
if (!RegisterClass(&wc)) | |
{ | |
MessageBox(NULL, "Error creating window class", NAME, MB_OK | MB_ICONERROR); | |
return 0; | |
} | |
if (!(hWnd = CreateWindow(NAME, NAME, WS_OVERLAPPED, 0, 0, 0, 0, 0, 0, hInstance, 0))) | |
{ | |
MessageBox(NULL, "Error creating window", NAME, MB_OK | MB_ICONERROR); | |
return 0; | |
} | |
while (IsWindow(hWnd) && GetMessage(&msg, hWnd, 0, 0)) | |
{ | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
return 0; | |
} | |
LRESULT CALLBACK HookWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
{ | |
if (msg == WM_DESTROY) | |
{ | |
UnRegisterHook(); | |
FreeLibrary(hLib); | |
PostQuitMessage(0); | |
} | |
return DefWindowProc(hWnd, msg, wParam, lParam); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment