|
1 | 1 | #include <Windows.h>
|
2 | 2 | #include <stdbool.h>
|
3 | 3 | #include <stdio.h>
|
4 |
| -// Author: https://github.com/spacemiqote |
| 4 | +//Author:https://github.com/spacemiqote |
5 | 5 |
|
6 |
| -HHOOK keyboardHook; |
7 | 6 | RECT cursorBounds;
|
8 | 7 | bool boundsEnabled = false;
|
9 |
| - |
10 |
| -void SetCursorBounds(int left, int top, int right, int bottom); |
| 8 | +void SetCursorBounds(int,int,int,int); |
11 | 9 | void ReleaseCursorBounds(void);
|
12 |
| -void SetCursorBoundsToWindow(HWND hwnd); |
| 10 | +void SetCursorBoundsToWindow(HWND); |
13 | 11 | void ToggleCursorBounds(void);
|
14 | 12 | void ExitApplication(void);
|
15 |
| -LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); |
16 |
| -void InstallKeyboardHook(void); |
17 |
| -void UninstallKeyboardHook(void); |
18 |
| -void c(void); |
| 13 | +void c(HINSTANCE hInstance); |
19 | 14 |
|
20 | 15 | void SetCursorBounds(int left, int top, int right, int bottom) {
|
21 |
| - cursorBounds.left = left; |
22 |
| - cursorBounds.top = top; |
23 |
| - cursorBounds.right = right; |
24 |
| - cursorBounds.bottom = bottom; |
25 |
| - ClipCursor(&cursorBounds); |
| 16 | + cursorBounds.left = left; |
| 17 | + cursorBounds.top = top; |
| 18 | + cursorBounds.right = right; |
| 19 | + cursorBounds.bottom = bottom; |
| 20 | + ClipCursor(&cursorBounds); |
26 | 21 | }
|
27 | 22 |
|
28 | 23 | void ReleaseCursorBounds() {
|
29 |
| - ClipCursor(NULL); |
30 |
| - boundsEnabled = false; |
| 24 | + ClipCursor(NULL); |
| 25 | + boundsEnabled = false; |
31 | 26 | }
|
32 | 27 |
|
33 | 28 | void SetCursorBoundsToWindow(HWND hwnd) {
|
34 |
| - RECT clientRect; |
35 |
| - GetClientRect(hwnd, &clientRect); |
36 |
| - MapWindowPoints(hwnd, NULL, (LPPOINT)&clientRect, 2); |
37 |
| - SetCursorBounds(clientRect.left, clientRect.top, clientRect.right, clientRect.bottom); |
38 |
| - boundsEnabled = true; |
39 |
| -} |
40 |
| - |
41 |
| -void UninstallKeyboardHook() { |
42 |
| - if (keyboardHook != NULL) { |
43 |
| - UnhookWindowsHookEx(keyboardHook); |
44 |
| - keyboardHook = NULL; |
45 |
| - } |
| 29 | + RECT clientRect; |
| 30 | + GetClientRect(hwnd, &clientRect); |
| 31 | + MapWindowPoints(hwnd, NULL, (LPPOINT)&clientRect, 2); |
| 32 | + SetCursorBounds(clientRect.left, clientRect.top, clientRect.right, clientRect.bottom); |
| 33 | + boundsEnabled = true; |
46 | 34 | }
|
47 | 35 |
|
48 |
| -void ToggleCursorBounds() { |
49 |
| - if (boundsEnabled) { |
50 |
| - ReleaseCursorBounds(); |
51 |
| - } else { |
52 |
| - HWND foregroundWindow = GetForegroundWindow(); |
53 |
| - if (foregroundWindow != NULL) { |
54 |
| - SetCursorBoundsToWindow(foregroundWindow); |
55 |
| - } |
56 |
| - } |
| 36 | +void ToggleCursorBounds(){ |
| 37 | + if (boundsEnabled){ |
| 38 | + ReleaseCursorBounds(); |
| 39 | + }else{ |
| 40 | + HWND foregroundWindow=GetForegroundWindow(); |
| 41 | + if(foregroundWindow!=NULL)SetCursorBoundsToWindow(foregroundWindow); |
| 42 | + } |
57 | 43 | }
|
58 | 44 |
|
59 | 45 | void ExitApplication() {
|
60 |
| - ReleaseCursorBounds(); |
61 |
| - UninstallKeyboardHook(); |
62 |
| - PostQuitMessage(0); |
| 46 | + ReleaseCursorBounds(); |
| 47 | + PostQuitMessage(0); |
63 | 48 | }
|
64 | 49 |
|
65 |
| -LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { |
66 |
| - if (nCode == HC_ACTION && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { |
67 |
| - KBDLLHOOKSTRUCT* pKeyboard = (KBDLLHOOKSTRUCT*)lParam; |
68 |
| - if (pKeyboard->vkCode == VK_F12 && (GetAsyncKeyState(VK_MENU) & 0x8000)) { |
69 |
| - ToggleCursorBounds(); |
70 |
| - } else if (pKeyboard->vkCode == VK_F11 && (GetAsyncKeyState(VK_MENU) & 0x8000)) { |
71 |
| - ExitApplication(); |
72 |
| - } |
73 |
| - } |
74 |
| - return CallNextHookEx(keyboardHook, nCode, wParam, lParam); |
| 50 | +LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){ |
| 51 | + if (uMsg==WM_INPUT){ |
| 52 | + UINT dwSize=0; |
| 53 | + GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER)); |
| 54 | + LPBYTE lpb=(LPBYTE)HeapAlloc(GetProcessHeap(),0,dwSize); |
| 55 | + if(lpb==NULL)return 0; |
| 56 | + if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb, |
| 57 | + &dwSize,sizeof(RAWINPUTHEADER))!=dwSize){ |
| 58 | + HeapFree(GetProcessHeap(),0,lpb); |
| 59 | + return 0; |
| 60 | + } |
| 61 | + RAWINPUT raw; |
| 62 | + CopyMemory(&raw,lpb,sizeof(RAWINPUT)); |
| 63 | + HeapFree(GetProcessHeap(),0,lpb); |
| 64 | + if(raw.header.dwType==RIM_TYPEKEYBOARD){ |
| 65 | + RAWKEYBOARD rawKeyboard=raw.data.keyboard; |
| 66 | + USHORT vkCode=rawKeyboard.VKey; |
| 67 | + if(rawKeyboard.Message==WM_KEYDOWN){ |
| 68 | + if(GetAsyncKeyState(VK_SHIFT)<0){ |
| 69 | + if(vkCode==VK_F11)ToggleCursorBounds(); |
| 70 | + else if(vkCode==VK_F12)ExitApplication(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + return DefWindowProc(hwnd, uMsg, wParam, lParam); |
75 | 76 | }
|
76 | 77 |
|
77 |
| -void InstallKeyboardHook() { |
78 |
| - keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, 0); |
| 78 | +void InstallRawInputHook(HWND hwnd){ |
| 79 | + RAWINPUTDEVICE rid[1]; |
| 80 | + rid[0].usUsagePage=1; |
| 81 | + rid[0].usUsage=6; |
| 82 | + rid[0].dwFlags=RIDEV_INPUTSINK; |
| 83 | + rid[0].hwndTarget=hwnd; |
| 84 | + if(!RegisterRawInputDevices(rid, 1, sizeof(rid[0])))return; |
79 | 85 | }
|
80 | 86 |
|
81 |
| -void c() { |
82 |
| - InstallKeyboardHook(); |
83 |
| - |
84 |
| - MSG msg; |
85 |
| - while (GetMessage(&msg, NULL, 0, 0)) { |
86 |
| - TranslateMessage(&msg); |
87 |
| - DispatchMessage(&msg); |
88 |
| - } |
| 87 | +HWND CreateMessageOnlyWindow(HINSTANCE hInstance) { |
| 88 | + WNDCLASS wc={0}; |
| 89 | + wc.lpfnWndProc=WindowProc; |
| 90 | + wc.hInstance=hInstance; |
| 91 | + wc.lpszClassName="CursorRestrict"; |
| 92 | + if(!RegisterClass(&wc))return NULL; |
| 93 | + HWND hwnd = CreateWindowEx( |
| 94 | + 0, |
| 95 | + "CursorRestrict", |
| 96 | + NULL, |
| 97 | + 0, |
| 98 | + 0,0,0,0, |
| 99 | + HWND_MESSAGE, NULL, hInstance, NULL |
| 100 | + ); |
| 101 | + if(!hwnd)return 0; |
| 102 | + return hwnd; |
| 103 | +} |
89 | 104 |
|
90 |
| - UninstallKeyboardHook(); |
| 105 | +void c(HINSTANCE hInstance){ |
| 106 | + HWND hwnd=CreateMessageOnlyWindow(hInstance); |
| 107 | + if(!hwnd)return; |
| 108 | + InstallRawInputHook(hwnd); |
| 109 | + MSG msg; |
| 110 | + while (GetMessage(&msg, NULL, 0, 0)) { |
| 111 | + TranslateMessage(&msg); |
| 112 | + DispatchMessage(&msg); |
| 113 | + } |
91 | 114 | }
|
0 commit comments