|
| 1 | +// MouseButtonClicker - http://blogs.msdn.com/Delay |
| 2 | +// |
| 3 | +// "MouseButtonClicker clicks the mouse so you don't have to!" |
| 4 | +// |
| 5 | +// Simple Windows utility to click the primary mouse button whenever the mouse |
| 6 | +// stops moving as a usability convenience/enhancement. |
| 7 | + |
| 8 | + |
| 9 | +#include <windows.h> |
| 10 | +#include <tchar.h> |
| 11 | +#include <malloc.h> |
| 12 | +#include <strsafe.h> |
| 13 | + |
| 14 | +// Macro for compile-time assert |
| 15 | +#define COMPILE_TIME_ASSERT(e) typedef int CTA[(e) ? 1 : -1] |
| 16 | + |
| 17 | +// Constants for code simplification |
| 18 | +#define RI_ALL_MOUSE_BUTTONS_DOWN (RI_MOUSE_BUTTON_1_DOWN | RI_MOUSE_BUTTON_2_DOWN | RI_MOUSE_BUTTON_3_DOWN | RI_MOUSE_BUTTON_4_DOWN | RI_MOUSE_BUTTON_5_DOWN) |
| 19 | +#define RI_ALL_MOUSE_BUTTONS_UP (RI_MOUSE_BUTTON_1_UP | RI_MOUSE_BUTTON_2_UP | RI_MOUSE_BUTTON_3_UP | RI_MOUSE_BUTTON_4_UP | RI_MOUSE_BUTTON_5_UP) |
| 20 | + |
| 21 | +// Check that bit-level assumptions are correct |
| 22 | +COMPILE_TIME_ASSERT(RI_MOUSE_BUTTON_1_DOWN == (RI_MOUSE_BUTTON_1_UP >> 1)); |
| 23 | +COMPILE_TIME_ASSERT(RI_MOUSE_BUTTON_2_DOWN == (RI_MOUSE_BUTTON_2_UP >> 1)); |
| 24 | +COMPILE_TIME_ASSERT(RI_MOUSE_BUTTON_3_DOWN == (RI_MOUSE_BUTTON_3_UP >> 1)); |
| 25 | +COMPILE_TIME_ASSERT(RI_MOUSE_BUTTON_4_DOWN == (RI_MOUSE_BUTTON_4_UP >> 1)); |
| 26 | +COMPILE_TIME_ASSERT(RI_MOUSE_BUTTON_5_DOWN == (RI_MOUSE_BUTTON_5_UP >> 1)); |
| 27 | +COMPILE_TIME_ASSERT(RI_ALL_MOUSE_BUTTONS_DOWN == (RI_ALL_MOUSE_BUTTONS_UP >> 1)); |
| 28 | + |
| 29 | +// Macro for absolute value |
| 30 | +#define ABS(v) ((0 <= v) ? v : -v) |
| 31 | + |
| 32 | +// Application-level constants |
| 33 | +#define APPLICATION_NAME (TEXT("MouseButtonClicker")) |
| 34 | +#define TIMER_EVENT_ID (1) |
| 35 | +#define MOUSE_MOVE_THRESHOLD (2) |
| 36 | + |
| 37 | +// Window procedure |
| 38 | +LRESULT CALLBACK WndProc(const HWND hWnd, const UINT message, const WPARAM wParam, const LPARAM lParam) |
| 39 | +{ |
| 40 | + // Tracks the mouse move delta threshold across calls to WndProc |
| 41 | + static LONG lLastClickDeltaX = 0; |
| 42 | + static LONG lLastClickDeltaY = 0; |
| 43 | + static bool fOkToClick = false; |
| 44 | + |
| 45 | + switch (message) |
| 46 | + { |
| 47 | + // Raw input message |
| 48 | + case WM_INPUT: |
| 49 | + { |
| 50 | + // Query for required buffer size |
| 51 | + UINT cbSize = 0; |
| 52 | + if(0 == GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, NULL, &cbSize, sizeof(RAWINPUTHEADER))) |
| 53 | + { |
| 54 | + // Allocate buffer on stack (falls back to heap) |
| 55 | + const LPVOID pData = _malloca(cbSize); |
| 56 | + if(NULL != pData) |
| 57 | + { |
| 58 | + // Get raw input data |
| 59 | + if(cbSize == GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, pData, &cbSize, sizeof(RAWINPUTHEADER))) |
| 60 | + { |
| 61 | + // Only interested in mouse input |
| 62 | + const RAWINPUT* const pRawInput = static_cast<LPRAWINPUT>(pData); |
| 63 | + if (RIM_TYPEMOUSE == pRawInput->header.dwType) |
| 64 | + { |
| 65 | + // Only interested in devices that use relative coordinates |
| 66 | + // Specifically, input from pens/tablets is ignored |
| 67 | + if(0 == (pRawInput->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)) |
| 68 | + { |
| 69 | + // Tracks the state of the mouse buttons across calls to WndProc |
| 70 | + static UINT usMouseButtonsDown = 0; |
| 71 | + |
| 72 | + // Update mouse delta variables |
| 73 | + lLastClickDeltaX += pRawInput->data.mouse.lLastX; |
| 74 | + lLastClickDeltaY += pRawInput->data.mouse.lLastY; |
| 75 | + |
| 76 | + // Enable clicking once the mouse has exceeded the threshold in any direction |
| 77 | + fOkToClick |= ((MOUSE_MOVE_THRESHOLD < ABS(lLastClickDeltaX)) || (MOUSE_MOVE_THRESHOLD < ABS(lLastClickDeltaY))); |
| 78 | + |
| 79 | + // Determine the input type |
| 80 | + const UINT usButtonFlags = pRawInput->data.mouse.usButtonFlags; |
| 81 | + if(0 == (usButtonFlags & (RI_ALL_MOUSE_BUTTONS_DOWN | RI_ALL_MOUSE_BUTTONS_UP | RI_MOUSE_WHEEL))) |
| 82 | + { |
| 83 | + // Mouse move: (Re)set click timer if no buttons down and mouse moved enough to avoid jitter |
| 84 | + if((0 == usMouseButtonsDown) && fOkToClick) |
| 85 | + { |
| 86 | + // Use double-click time as an indication of the user's responsiveness preference |
| 87 | + (void)SetTimer(hWnd, TIMER_EVENT_ID, GetDoubleClickTime(), NULL); |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + // Mouse button down/up or wheel rotation: Cancel click timer |
| 93 | + (void)KillTimer(hWnd, TIMER_EVENT_ID); |
| 94 | + |
| 95 | + // Update mouse button state variable (asserts above ensure the bit manipulations are correct) |
| 96 | + usMouseButtonsDown |= (usButtonFlags & RI_ALL_MOUSE_BUTTONS_DOWN); |
| 97 | + usMouseButtonsDown &= ~((usButtonFlags & RI_ALL_MOUSE_BUTTONS_UP) >> 1); |
| 98 | + |
| 99 | + // Reset mouse delta and threshold variables |
| 100 | + lLastClickDeltaX = 0; |
| 101 | + lLastClickDeltaY = 0; |
| 102 | + fOkToClick = false; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + // Free buffer |
| 108 | + (void)_freea(pData); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + break; |
| 113 | + // Timer message |
| 114 | + case WM_TIMER: |
| 115 | + { |
| 116 | + // Timeout, stop timer and click primary button |
| 117 | + (void)KillTimer(hWnd, TIMER_EVENT_ID); |
| 118 | + INPUT pInputs[2] = {0}; |
| 119 | + pInputs[0].type = INPUT_MOUSE; |
| 120 | + pInputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; |
| 121 | + pInputs[1].type = INPUT_MOUSE; |
| 122 | + pInputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP; |
| 123 | + (void)SendInput(2, pInputs, sizeof(INPUT)); |
| 124 | + |
| 125 | + // Reset mouse delta and threshold variables |
| 126 | + lLastClickDeltaX = 0; |
| 127 | + lLastClickDeltaY = 0; |
| 128 | + fOkToClick = false; |
| 129 | + } |
| 130 | + break; |
| 131 | + // Close message |
| 132 | + case WM_DESTROY: |
| 133 | + (void)PostQuitMessage(0); |
| 134 | + break; |
| 135 | + // Unhandled message |
| 136 | + default: |
| 137 | + return DefWindowProc(hWnd, message, wParam, lParam); |
| 138 | + } |
| 139 | + // Return value 0 indicates message was processed |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +// WinMain entry point |
| 144 | +int APIENTRY _tWinMain(const HINSTANCE hInstance, const HINSTANCE hPrevInstance, const LPTSTR lpCmdLine, const int nCmdShow) |
| 145 | +{ |
| 146 | + // Avoid compiler warnings for unreferenced parameters |
| 147 | + UNREFERENCED_PARAMETER(hPrevInstance); |
| 148 | + UNREFERENCED_PARAMETER(lpCmdLine); |
| 149 | + UNREFERENCED_PARAMETER(nCmdShow); |
| 150 | + |
| 151 | + // Create a mutex to prevent running multiple simultaneous instances |
| 152 | + const HANDLE mutex = CreateMutex(NULL, FALSE, APPLICATION_NAME); |
| 153 | + if((NULL != mutex) && (ERROR_ALREADY_EXISTS != GetLastError())) |
| 154 | + { |
| 155 | + // Register the window class |
| 156 | + WNDCLASS wc = {0}; |
| 157 | + wc.lpfnWndProc = WndProc; |
| 158 | + wc.hInstance = hInstance; |
| 159 | + wc.lpszClassName = APPLICATION_NAME; |
| 160 | + if(0 != RegisterClass(&wc)) |
| 161 | + { |
| 162 | + // Create a message-only window to receive WM_INPUT and WM_TIMER |
| 163 | + const HWND hWnd = CreateWindow(APPLICATION_NAME, APPLICATION_NAME, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_MESSAGE, NULL, hInstance, NULL); |
| 164 | + if (NULL != hWnd) |
| 165 | + { |
| 166 | + // Register for the mouse's raw input data |
| 167 | + RAWINPUTDEVICE rid = {0}; |
| 168 | + rid.usUsagePage = 1; // HID_DEVICE_SYSTEM_MOUSE |
| 169 | + rid.usUsage = 2; // HID_DEVICE_SYSTEM_MOUSE |
| 170 | + rid.dwFlags = RIDEV_INPUTSINK; |
| 171 | + rid.hwndTarget = hWnd; |
| 172 | + if(RegisterRawInputDevices(&rid, 1, sizeof(rid))) |
| 173 | + { |
| 174 | + // Pump Windows messages |
| 175 | + MSG msg = {0}; |
| 176 | + while (GetMessage(&msg, NULL, 0, 0)) |
| 177 | + { |
| 178 | + TranslateMessage(&msg); |
| 179 | + DispatchMessage(&msg); |
| 180 | + } |
| 181 | + // Return success |
| 182 | + return static_cast<int>(msg.wParam); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + // Failed to initialize, output a diagnostic message (which is not more |
| 187 | + // friendly because it represents a scenario that should never occur) |
| 188 | + TCHAR szMessage[64]; |
| 189 | + if(SUCCEEDED(StringCchPrintf(szMessage, sizeof(szMessage)/sizeof(szMessage[0]), TEXT("Initialization failure. GetLastError=%d\r\n"), GetLastError()))) |
| 190 | + { |
| 191 | + (void)MessageBox(NULL, szMessage, APPLICATION_NAME, MB_OK | MB_ICONERROR); |
| 192 | + } |
| 193 | + } |
| 194 | + // Return failure |
| 195 | + return 0; |
| 196 | + // By contract, Windows frees all resources as part of process exit |
| 197 | +} |
0 commit comments