├── .gitignore ├── AutoMouseLock.cpp ├── AutoMouseLock.manifest ├── Makefile └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | *.obj 3 | -------------------------------------------------------------------------------- /AutoMouseLock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | 7 | BOOL wasFullScreen = FALSE; 8 | int lastCheck = 0; 9 | 10 | inline void check() 11 | { 12 | // Don't check more than once per tick, 13 | // to lessen CPU load 14 | int now = GetTickCount(); 15 | if (lastCheck == now) 16 | return; 17 | lastCheck = now; 18 | 19 | HWND fg = GetForegroundWindow(); 20 | RECT fgRect; 21 | GetClientRect(fg, &fgRect); 22 | 23 | int monW = GetSystemMetrics(SM_CXSCREEN); 24 | int monH = GetSystemMetrics(SM_CYSCREEN); 25 | 26 | #ifdef DEBUG 27 | printf("%d %d %d %d %d %d\n", 28 | fg, 29 | fg != GetDesktopWindow(), 30 | fgRect.left == 0, 31 | fgRect.top == 0, 32 | fgRect.right == monW, 33 | fgRect.bottom == monH 34 | ); 35 | 36 | #endif 37 | 38 | BOOL fullScreen = 39 | fg && 40 | fg != GetDesktopWindow() && 41 | fgRect.left == 0 && 42 | fgRect.top == 0 && 43 | fgRect.right == monW && 44 | fgRect.bottom == monH; 45 | 46 | if (fullScreen != wasFullScreen) 47 | { 48 | #ifdef DEBUG 49 | printf("%s!\n", fullScreen ? "Locking" : "Unlocking"); 50 | #endif 51 | 52 | BOOL ok = ClipCursor(fullScreen ? &fgRect : NULL); 53 | 54 | #ifdef DEBUG 55 | printf("%s!\n", ok ? "Success" : "Failed"); 56 | #endif 57 | 58 | wasFullScreen = fullScreen; 59 | } 60 | } 61 | 62 | // Use a low-level mouse hook to get around Windows restrictions 63 | // Method credit to http://stackoverflow.com/a/12027257/21501 64 | 65 | HHOOK hMouseHook; 66 | 67 | LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) 68 | { 69 | check(); 70 | return CallNextHookEx(hMouseHook, nCode, wParam, lParam); 71 | } 72 | 73 | void main() 74 | { 75 | hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, &LowLevelMouseProc, GetModuleHandle(NULL), 0); 76 | 77 | MSG msg; 78 | while (GetMessage(&msg, NULL, 0, 0) > 0) 79 | { 80 | TranslateMessage(&msg); 81 | DispatchMessage(&msg); 82 | } 83 | 84 | UnhookWindowsHookEx(hMouseHook); 85 | ClipCursor(NULL); 86 | } 87 | -------------------------------------------------------------------------------- /AutoMouseLock.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | AutoMouseLock.exe : AutoMouseLock.obj Makefile 2 | link /nologo /ENTRY:main /NODEFAULTLIB /SUBSYSTEM:WINDOWS /MERGE:.text=.cyberml /MERGE:.rdata=.cyberml /MERGE:.data=.cyberml /SECTION:.cyberml,ERW AutoMouseLock.obj user32.lib kernel32.lib 3 | # ulink -Tpd AutoMouseLock.obj msvcrt.lib -e_DllMain@12 kernel32.lib user32.lib 4 | AutoMouseLock.obj : AutoMouseLock.cpp 5 | cl /nologo /c /O1 /GS- AutoMouseLock.cpp 6 | 7 | debug: 8 | cl -DDEBUG AutoMouseLock.cpp user32.lib 9 | 10 | AutoMouseLock-manifest.exe : AutoMouseLock.exe AutoMouseLock.manifest 11 | cp -f AutoMouseLock.exe AutoMouseLock-tmp.exe 12 | mt -manifest AutoMouseLock.manifest -outputresource:AutoMouseLock-tmp.exe 13 | mv -f AutoMouseLock-tmp.exe AutoMouseLock-manifest.exe 14 | 15 | AutoMouseLock-signed.exe : AutoMouseLock-manifest.exe 16 | cp -f AutoMouseLock-manifest.exe AutoMouseLock-tmp.exe 17 | signtool sign /n "Vladimir Panteleev" /d "AutoMouseLock" /du "https://github.com/CyberShadow/AutoMouseLock" /t http://time.certum.pl/ AutoMouseLock-tmp.exe 18 | mv -f AutoMouseLock-tmp.exe AutoMouseLock-signed.exe 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AutoMouseLock 2 | ============= 3 | 4 | This program will automatically restrict the mouse cursor to your main monitor, 5 | as long as a full-screen program is open on it. 6 | 7 | ### Download 8 | 9 | The program consists of a single 8KB .exe file. 10 | 11 | You can download it from my website: 12 | 13 | https://files.thecybershadow.net/AutoMouseLock/ 14 | 15 | ### Usage 16 | 17 | Just run it. 18 | 19 | The program has no interface. 20 | As it is designed to only activate automatically, it should not get in your way, and you can leave it running. 21 | To close it, just kill the process from the Windows Task Manager. 22 | --------------------------------------------------------------------------------