Last active
March 6, 2021 15:47
-
-
Save preetpalS/81405cd78ade738034cfa6d49e2a4202 to your computer and use it in GitHub Desktop.
AutoHotKey script replacement in C for multi-monitor window maximization (Windows only)
This file contains 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
// Compile with: cl /O2 /GL src\main.c | |
#include "stdbool.h" | |
// #include "stdio.h" | |
#include "windows.h" | |
#include "winuser.h" | |
#pragma comment( lib, "user32.lib") | |
bool altPressed = false; | |
bool controlPressed = false; | |
bool shiftPressed = false; | |
bool winkeyPressed = false; | |
LRESULT WINAPI hookProc(_In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM lParam); | |
int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE _hPrevInstance, LPSTR _lpCmdLine, int _nCmdShow) | |
{ | |
int returnCode = 0; | |
// Ensures only one instance of application can run at one time | |
HANDLE globalMutexHandle = CreateMutex(NULL, true, "multi-monitor-window-maximization"); | |
if (globalMutexHandle == NULL) { | |
returnCode = -1; | |
goto exit; | |
} | |
DWORD lastErrorAfterMutexCreationAttempt = GetLastError(); | |
if (lastErrorAfterMutexCreationAttempt == ERROR_ALREADY_EXISTS) { | |
returnCode = -2; | |
goto cleanup; | |
} if (lastErrorAfterMutexCreationAttempt == ERROR_INVALID_HANDLE) { | |
returnCode = -3; | |
goto cleanup; | |
} | |
HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)&hookProc, NULL, 0); | |
if (hook == NULL) { | |
// printf("Exited with code : %u", GetLastError()); | |
returnCode = -4; | |
goto cleanup; | |
} | |
MSG msg; | |
while(GetMessage(&msg, NULL, 0u, 0u)) { | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
UnhookWindowsHookEx(hook); | |
cleanup: | |
ReleaseMutex(globalMutexHandle); | |
CloseHandle(globalMutexHandle); | |
exit: | |
return returnCode; | |
} | |
void changeWindowPosition(int x, int y, int width, int height) { | |
HWND foreground = GetForegroundWindow(); | |
if (foreground == NULL) { | |
// printf("No active window to maximize.\n"); | |
return; | |
} | |
int _wasPreviouslyVisibleP = ShowWindow(foreground, SW_RESTORE); | |
int _wasWindowResizedP = SetWindowPos(foreground, HWND_NOTOPMOST, | |
x, y, width, height, | |
SWP_ASYNCWINDOWPOS); | |
// if (wasWindowResizedP == 0) { | |
// printf("Failed to change window size and/or position\n"); | |
// } | |
} | |
void superMaximizeCurrentWindow() { | |
changeWindowPosition(0, 0, 2560, 2520); | |
} | |
void ultraMaximizeCurrentWindow() { | |
changeWindowPosition(0, 0, 4000, 2520); | |
} | |
void superLeftMaximizeCurrentWindow() { | |
changeWindowPosition(0, 0, 1280, 2520); | |
} | |
void superRightMaximizeCurrentWindow() { | |
changeWindowPosition(1280, 0, 1280, 2520); | |
} | |
bool detectAndActivateShortcut(DWORD vkCode) | |
{ | |
if (winkeyPressed && shiftPressed) { | |
switch (vkCode) { | |
case VK_UP: | |
if (controlPressed) { | |
ultraMaximizeCurrentWindow(); | |
} else { | |
superMaximizeCurrentWindow(); | |
} | |
return true; | |
case VK_LEFT: | |
superLeftMaximizeCurrentWindow(); | |
return true; | |
case VK_RIGHT: | |
superRightMaximizeCurrentWindow(); | |
return true; | |
default: | |
break; | |
} | |
} | |
return false; | |
} | |
LRESULT WINAPI hookProc(_In_ int nCode, _In_ WPARAM wParam, _In_ LPARAM lParam) | |
{ | |
if(nCode < 0) | |
return CallNextHookEx(NULL, nCode, wParam, lParam); | |
bool activated = false; | |
KBDLLHOOKSTRUCT* hookStruct = (KBDLLHOOKSTRUCT*) lParam; | |
if(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) | |
{ | |
// printf("Pressed : %d\n", hookStruct->vkCode); | |
switch (hookStruct->vkCode) { | |
case VK_LSHIFT: // 0xA1 Left Shift Key | |
case VK_RSHIFT: // 0xA2 Rigth Shift Key | |
shiftPressed = true; | |
// printf("Shift pressed"); | |
break; | |
case VK_LCONTROL: // 0xA3 Left Control Key | |
case VK_RCONTROL: // 0xA4 Left Control Key | |
controlPressed = true; | |
break; | |
case VK_LMENU: // 0xA5 Left Alt Key | |
case VK_RMENU: // 0xA6 Right Alt Key | |
altPressed = true; | |
break; | |
case VK_LWIN: // 0x5B Windows key | |
case VK_RWIN: // 0x5C Windows key | |
winkeyPressed = true; | |
break; | |
case VK_SHIFT: // 0x10 Shift Key | |
shiftPressed = true; | |
// printf("Shift pressed"); | |
break; | |
case VK_CONTROL: // 0x11 Control Key | |
controlPressed = true; | |
break; | |
case VK_MENU: // 0x12 Alt Key | |
altPressed = true; | |
break; | |
default: | |
activated = detectAndActivateShortcut(hookStruct->vkCode); | |
break; | |
} | |
} | |
else if(wParam == WM_KEYUP || wParam == WM_SYSKEYUP) | |
{ | |
// printf("Released : %d\n", hookStruct->vkCode); | |
switch (hookStruct->vkCode) { | |
case VK_LSHIFT: // 0xA1 Left Shift Key | |
case VK_RSHIFT: // 0xA2 Rigth Shift Key | |
shiftPressed = false; | |
// printf("Shift released"); | |
break; | |
case VK_LCONTROL: // 0xA3 Left Control Key | |
case VK_RCONTROL: // 0xA4 Left Control Key | |
controlPressed = false; | |
break; | |
case VK_LMENU: // 0xA5 Left Alt Key | |
case VK_RMENU: // 0xA6 Right Alt Key | |
altPressed = false; | |
break; | |
case VK_LWIN: // 0x5B Windows key | |
case VK_RWIN: // 0x5C Windows key | |
winkeyPressed = false; | |
break; | |
case VK_SHIFT: // 0x10 Shift Key | |
shiftPressed = false; | |
// printf("Shift released"); | |
break; | |
case VK_CONTROL: // 0x11 Control Key | |
controlPressed = false; | |
break; | |
case VK_MENU: // 0x12 Alt Key | |
altPressed = false; | |
break; | |
default: | |
break; | |
} | |
} | |
if (activated) { | |
return -1; | |
} else { | |
return CallNextHookEx(NULL, (activated ? -1 : nCode), wParam, lParam); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment