This works almost perfectly for me intended use case. However, one thing I noticed is…
- User
This works almost perfectly for me intended use case. However, one thing I noticed is that when I start the .exe after building and put the cursor somewhere and start scroll, the next time I scroll the cursor jumps to that position. I'd prefer if the cursor just always stays where I put it last time. Can you fix it and output back to me the complete updated script such that I can copy paste it and try if the fix worked? Here's the script: #include <windows.h> #include // For timing #include // For std::abs #include #include #include #include #include
std::string trackpointDevicePath; std::string hDevice; bool isPreConfigured = false; const int TOLERANCE = 5; const int REST_TOLERANCE = 10;
const int SCROLL_SENSITIVITY = 120; const double INTENSITY_FACTOR = 0.15;
std::chrono::steady_clock::time_point lastScrollTime; const std::chrono::milliseconds SCROLL_INTERVAL(50);
int currentXPressure = 0; int currentYPressure = 0; HWND targetScrollWindow = NULL;
POINT lockedCursorPos; bool cursorLocked = false;
std::string hexify(HANDLE h) { std::stringstream ss; ss << std::hex << h; return ss.str(); }
void ScrollWindow(HWND targetWindow, int xAmount, int yAmount) { if (!targetWindow) return;
POINT clientPos = lockedCursorPos; // Use the locked cursor position ScreenToClient(targetWindow, &clientPos); if (yAmount != 0) { SendMessage(targetWindow, WM_MOUSEWHEEL, MAKEWPARAM(0, -yAmount), MAKELPARAM(clientPos.x, clientPos.y)); } if (xAmount != 0) { SendMessage(targetWindow, WM_MOUSEHWHEEL, MAKEWPARAM(0, xAmount), MAKELPARAM(clientPos.x, clientPos.y)); }}
VOID CALLBACK ScrollTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { int xScroll = static_cast(currentXPressure * INTENSITY_FACTOR * SCROLL_SENSITIVITY); int yScroll = static_cast(currentYPressure * INTENSITY_FACTOR * SCROLL_SENSITIVITY);
if (targetScrollWindow && (xScroll != 0 || yScroll != 0)) { // Check if the current window under lockedCursorPos has changed HWND currentWindow = WindowFromPoint(lockedCursorPos); if (currentWindow != targetScrollWindow) { targetScrollWindow = currentWindow; } ScrollWindow(targetScrollWindow, xScroll, yScroll); }}
void ProcessTrackPointMovement(RAWINPUT* raw) { int xMovement = raw->data.mouse.lLastX; int yMovement = raw->data.mouse.lLastY;
if (std::abs(xMovement) < TOLERANCE) xMovement = 0; if (std::abs(yMovement) < TOLERANCE) yMovement = 0; currentXPressure = xMovement; currentYPressure = yMovement; bool atRest = (std::abs(xMovement) < REST_TOLERANCE) && (std::abs(yMovement) < REST_TOLERANCE); if (atRest) { currentXPressure = 0; currentYPressure = 0; } else { if (!targetScrollWindow) { GetCursorPos(&lockedCursorPos); targetScrollWindow = WindowFromPoint(lockedCursorPos); cursorLocked = true; } if (cursorLocked) { SetCursorPos(lockedCursorPos.x, lockedCursorPos.y); } }}
void setMovement(std::string devicePath, RAWINPUT* raw) { if (trackpointDevicePath == devicePath && hDevice == hexify(raw->header.hDevice)) { ProcessTrackPointMovement(raw); } }
bool RegisterTrackPoint(HWND hwnd) { RAWINPUTDEVICE rid[1];
rid[0].usUsagePage = 0x01; rid[0].usUsage = 0x02; rid[0].dwFlags = RIDEV_INPUTSINK | RIDEV_NOLEGACY; rid[0].hwndTarget = hwnd; if (RegisterRawInputDevices(rid, 1, sizeof(rid[0])) == FALSE) { std::cerr << "Failed to register raw input device." << std::endl; return false; } return true;}
void IdentifyTrackPoint() { UINT numDevices; GetRawInputDeviceList(nullptr, &numDevices, sizeof(RAWINPUTDEVICELIST)); std::vector devices(numDevices); GetRawInputDeviceList(devices.data(), &numDevices, sizeof(RAWINPUTDEVICELIST));
std::cout << "Listing all input devices:" << std::endl; std::string tempPath = "0"; for (UINT i = 0; i < numDevices; ++i) { char devicePath[256]; UINT pathSize = sizeof(devicePath); GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, devicePath, &pathSize); std::string deviceType; switch (devices[i].dwType) { case RIM_TYPEMOUSE: deviceType = "Mouse"; break; case RIM_TYPEKEYBOARD: deviceType = "Keyboard"; break; case RIM_TYPEHID: deviceType = "HID"; break; default: deviceType = "Unknown"; break; } if (!isPreConfigured) { std::cout << "Device " << i + 1 << ": " << devicePath << " (" << deviceType << ")" << " [" << devices[i].hDevice << "]" << std::endl; } if (isPreConfigured && trackpointDevicePath == devicePath && hDevice == hexify(devices[i].hDevice)) { tempPath = devicePath; std::cout << "TrackPoint Device Found" << std::endl; break; } } if (!isPreConfigured) { std::cout << "\nMove Trackpoint\n" << std::endl; } if (isPreConfigured && tempPath == "0") { std::cout << "TrackPoint Device Not Found\nDelete config.ini and try again" << std::endl; }}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_INPUT) { UINT dwSize = 0; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL) return 0; if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize) { std::cerr << "GetRawInputData does not return correct size!" << std::endl; } RAWINPUT* raw = (RAWINPUT*)lpb; if (raw->header.dwType == RIM_TYPEMOUSE) { char devicePath[256]; UINT pathSize = sizeof(devicePath); GetRawInputDeviceInfoA(raw->header.hDevice, RIDI_DEVICENAME, devicePath, &pathSize); if (!isPreConfigured) { std::cout << "Details of Input: " << std::endl; std::cout << "Device Path : " << devicePath << std::endl; std::cout << "hDevice : " << raw->header.hDevice << std::endl; std::ofstream settingsFile("config.ini"); settingsFile << "devicePath=" << devicePath << std::endl; settingsFile << "hDevice=" << raw->header.hDevice << std::endl; settingsFile.close(); trackpointDevicePath = devicePath; hDevice = hexify(raw->header.hDevice); isPreConfigured = true; std::cout << "Config is saved." << std::endl; return 0; } else { setMovement(devicePath, raw); } } delete[] lpb; return 0; } else if (uMsg == WM_LBUTTONDOWN || uMsg == WM_RBUTTONDOWN || uMsg == WM_MBUTTONDOWN) { targetScrollWindow = NULL; cursorLocked = false; } return DefWindowProc(hwnd, uMsg, wParam, lParam);}
int main() { std::cout << "*** Thinkpad Trackpoint Scroll Utility ***" << std::endl;
std::cout << "Loading config.ini" << std::endl; std::ifstream settingsFile("config.ini"); if (settingsFile.is_open()) { std::string line; while (std::getline(settingsFile, line)) { if (line.find("devicePath") != std::string::npos) { trackpointDevicePath = line.substr(line.find("=") + 1); } if (line.find("hDevice") != std::string::npos) { hDevice = line.substr(line.find("=") + 1); } if (!trackpointDevicePath.empty() && !hDevice.empty()) { isPreConfigured = true; break; } } settingsFile.close(); } else { std::cout << "Config is not found. Creating config.ini" << std::endl; std::cout << "Press Enter to Configure TrackPoint: After pressing Enter, only move the TrackPoint." << std::endl; std::cin.get(); } WNDCLASS wc = { 0 }; wc.lpfnWndProc = WindowProc; wc.hInstance = GetModuleHandle(NULL); wc.lpszClassName = "TrackPointScrollClass"; RegisterClass(&wc); HWND hwnd = CreateWindowEx( 0, wc.lpszClassName, "TrackPoint Scroll Utility", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, wc.hInstance, NULL); if (!hwnd) { std::cerr << "Failed to create window." << std::endl; return 1; } IdentifyTrackPoint(); if (!RegisterTrackPoint(hwnd)) { std::cerr << "Failed to register TrackPoint." << std::endl; return 1; } SetTimer(hwnd, 1, SCROLL_INTERVAL.count(), ScrollTimerProc); std::cout << "TrackPoint Scroll Utility is running" << std::endl; std::cout << "Position your cursor over a window and hold the TrackPoint to scroll:" << std::endl; std::cout << " - Up/Down: Vertical scroll" << std::endl; std::cout << " - Left/Right: Horizontal scroll" << std::endl; std::cout << " - Push harder for faster scrolling" << std::endl; std::cout << " - Click to reset the scroll target window" << std::endl; MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } KillTimer(hwnd, 1); return 0;}
- Assistant
Certainly! The issue is that when you start scrolling, the code locks the cursor position to the current cursor position at that moment (
lockedCursorPos), but it never updateslockedCursorPosagain. So if you move the cursor manually after starting the program, the scroll target window and cursor position remain locked to the old position, causing the cursor to jump back when you scroll again.What you want:
- The cursor should stay where you last put it (i.e., the last manual cursor position), not jump back to the initial locked position when you start scrolling.
- So, you want to update
lockedCursorPosonly when the user moves the mouse manually (not when the program moves the cursor). - Also, when the user clicks (mouse button down), you reset the scroll target and unlock the cursor, so the next scroll locks the cursor at the current position again.
How to fix:
- Update
lockedCursorPosonly when the user moves the mouse manually, i.e., when the cursor moves without scrolling. - Do not forcibly reset the cursor position every time in
ProcessTrackPointMovementif the cursor is already locked. - Update
lockedCursorPoson mouse move messages (WM_MOUSEMOVE) or when the user moves the mouse manually. - When the user clicks (already handled), reset the lock so the next scroll locks the cursor at the new position.
Implementation details:
- Add handling for
WM_MOUSEMOVEinWindowProcto updatelockedCursorPosand unlock the cursor lock. - In
ProcessTrackPointMovement, only lock the cursor position if it is not locked yet. - Remove the repeated
SetCursorPoscalls that force the cursor to jump back every time you scroll. - This way, the cursor stays where the user last put it, and scrolling happens relative to that position.
Here is the complete updated script with the fix applied:
#include <windows.h> #include <chrono> // For timing #include <cmath> // For std::abs #include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> std::string trackpointDevicePath; std::string hDevice; bool isPreConfigured = false; const int TOLERANCE = 5; const int REST_TOLERANCE = 10; const int SCROLL_SENSITIVITY = 120; const double INTENSITY_FACTOR = 0.15; std::chrono::steady_clock::time_point lastScrollTime; const std::chrono::milliseconds SCROLL_INTERVAL(50); int currentXPressure = 0; int currentYPressure = 0; HWND targetScrollWindow = NULL; POINT lockedCursorPos; bool cursorLocked = false; std::string hexify(HANDLE h) { std::stringstream ss; ss << std::hex << h; return ss.str(); } void ScrollWindow(HWND targetWindow, int xAmount, int yAmount) { if (!targetWindow) return; POINT clientPos = lockedCursorPos; // Use the locked cursor position ScreenToClient(targetWindow, &clientPos); if (yAmount != 0) { SendMessage(targetWindow, WM_MOUSEWHEEL, MAKEWPARAM(0, -yAmount), MAKELPARAM(clientPos.x, clientPos.y)); } if (xAmount != 0) { SendMessage(targetWindow, WM_MOUSEHWHEEL, MAKEWPARAM(0, xAmount), MAKELPARAM(clientPos.x, clientPos.y)); } } VOID CALLBACK ScrollTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { int xScroll = static_cast<int>(currentXPressure * INTENSITY_FACTOR * SCROLL_SENSITIVITY); int yScroll = static_cast<int>(currentYPressure * INTENSITY_FACTOR * SCROLL_SENSITIVITY); if (targetScrollWindow && (xScroll != 0 || yScroll != 0)) { // Check if the current window under lockedCursorPos has changed HWND currentWindow = WindowFromPoint(lockedCursorPos); if (currentWindow != targetScrollWindow) { targetScrollWindow = currentWindow; } ScrollWindow(targetScrollWindow, xScroll, yScroll); } } void ProcessTrackPointMovement(RAWINPUT* raw) { int xMovement = raw->data.mouse.lLastX; int yMovement = raw->data.mouse.lLastY; if (std::abs(xMovement) < TOLERANCE) xMovement = 0; if (std::abs(yMovement) < TOLERANCE) yMovement = 0; currentXPressure = xMovement; currentYPressure = yMovement; bool atRest = (std::abs(xMovement) < REST_TOLERANCE) && (std::abs(yMovement) < REST_TOLERANCE); if (atRest) { currentXPressure = 0; currentYPressure = 0; } else { if (!cursorLocked) { // Lock cursor position only once when starting to scroll GetCursorPos(&lockedCursorPos); targetScrollWindow = WindowFromPoint(lockedCursorPos); cursorLocked = true; } // Do NOT forcibly reset cursor position every time to avoid jump // So remove SetCursorPos call here } } void setMovement(std::string devicePath, RAWINPUT* raw) { if (trackpointDevicePath == devicePath && hDevice == hexify(raw->header.hDevice)) { ProcessTrackPointMovement(raw); } } bool RegisterTrackPoint(HWND hwnd) { RAWINPUTDEVICE rid[1]; rid[0].usUsagePage = 0x01; rid[0].usUsage = 0x02; rid[0].dwFlags = RIDEV_INPUTSINK | RIDEV_NOLEGACY; rid[0].hwndTarget = hwnd; if (RegisterRawInputDevices(rid, 1, sizeof(rid[0])) == FALSE) { std::cerr << "Failed to register raw input device." << std::endl; return false; } return true; } void IdentifyTrackPoint() { UINT numDevices; GetRawInputDeviceList(nullptr, &numDevices, sizeof(RAWINPUTDEVICELIST)); std::vector<RAWINPUTDEVICELIST> devices(numDevices); GetRawInputDeviceList(devices.data(), &numDevices, sizeof(RAWINPUTDEVICELIST)); std::cout << "Listing all input devices:" << std::endl; std::string tempPath = "0"; for (UINT i = 0; i < numDevices; ++i) { char devicePath[256]; UINT pathSize = sizeof(devicePath); GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, devicePath, &pathSize); std::string deviceType; switch (devices[i].dwType) { case RIM_TYPEMOUSE: deviceType = "Mouse"; break; case RIM_TYPEKEYBOARD: deviceType = "Keyboard"; break; case RIM_TYPEHID: deviceType = "HID"; break; default: deviceType = "Unknown"; break; } if (!isPreConfigured) { std::cout << "Device " << i + 1 << ": " << devicePath << " (" << deviceType << ")" << " [" << devices[i].hDevice << "]" << std::endl; } if (isPreConfigured && trackpointDevicePath == devicePath && hDevice == hexify(devices[i].hDevice)) { tempPath = devicePath; std::cout << "TrackPoint Device Found" << std::endl; break; } } if (!isPreConfigured) { std::cout << "\nMove Trackpoint\n" << std::endl; } if (isPreConfigured && tempPath == "0") { std::cout << "TrackPoint Device Not Found\nDelete config.ini and try again" << std::endl; } } LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_INPUT) { UINT dwSize = 0; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); LPBYTE lpb = new BYTE[dwSize]; if (lpb == NULL) return 0; if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize) { std::cerr << "GetRawInputData does not return correct size!" << std::endl; } RAWINPUT* raw = (RAWINPUT*)lpb; if (raw->header.dwType == RIM_TYPEMOUSE) { char devicePath[256]; UINT pathSize = sizeof(devicePath); GetRawInputDeviceInfoA(raw->header.hDevice, RIDI_DEVICENAME, devicePath, &pathSize); if (!isPreConfigured) { std::cout << "Details of Input: " << std::endl; std::cout << "Device Path : " << devicePath << std::endl; std::cout << "hDevice : " << raw->header.hDevice << std::endl; std::ofstream settingsFile("config.ini"); settingsFile << "devicePath=" << devicePath << std::endl; settingsFile << "hDevice=" << raw->header.hDevice << std::endl; settingsFile.close(); trackpointDevicePath = devicePath; hDevice = hexify(raw->header.hDevice); isPreConfigured = true; std::cout << "Config is saved." << std::endl; delete[] lpb; return 0; } else { setMovement(devicePath, raw); } } delete[] lpb; return 0; } else if (uMsg == WM_LBUTTONDOWN || uMsg == WM_RBUTTONDOWN || uMsg == WM_MBUTTONDOWN) { // Reset scroll target and unlock cursor lock on mouse click targetScrollWindow = NULL; cursorLocked = false; } else if (uMsg == WM_MOUSEMOVE) { // Update lockedCursorPos and unlock cursor lock on manual mouse move POINT pt; GetCursorPos(&pt); lockedCursorPos = pt; cursorLocked = false; targetScrollWindow = NULL; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } int main() { std::cout << "*** Thinkpad Trackpoint Scroll Utility ***" << std::endl; std::cout << "Loading config.ini" << std::endl; std::ifstream settingsFile("config.ini"); if (settingsFile.is_open()) { std::string line; while (std::getline(settingsFile, line)) { if (line.find("devicePath") != std::string::npos) { trackpointDevicePath = line.substr(line.find("=") + 1); } if (line.find("hDevice") != std::string::npos) { hDevice = line.substr(line.find("=") + 1); } if (!trackpointDevicePath.empty() && !hDevice.empty()) { isPreConfigured = true; break; } } settingsFile.close(); } else { std::cout << "Config is not found. Creating config.ini" << std::endl; std::cout << "Press Enter to Configure TrackPoint: After pressing Enter, only move the TrackPoint." << std::endl; std::cin.get(); } WNDCLASS wc = { 0 }; wc.lpfnWndProc = WindowProc; wc.hInstance = GetModuleHandle(NULL); wc.lpszClassName = "TrackPointScrollClass"; RegisterClass(&wc); HWND hwnd = CreateWindowEx( 0, wc.lpszClassName, "TrackPoint Scroll Utility", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, wc.hInstance, NULL); if (!hwnd) { std::cerr << "Failed to create window." << std::endl; return 1; } IdentifyTrackPoint(); if (!RegisterTrackPoint(hwnd)) { std::cerr << "Failed to register TrackPoint." << std::endl; return 1; } SetTimer(hwnd, 1, SCROLL_INTERVAL.count(), ScrollTimerProc); std::cout << "TrackPoint Scroll Utility is running" << std::endl; std::cout << "Position your cursor over a window and hold the TrackPoint to scroll:" << std::endl; std::cout << " - Up/Down: Vertical scroll" << std::endl; std::cout << " - Left/Right: Horizontal scroll" << std::endl; std::cout << " - Push harder for faster scrolling" << std::endl; std::cout << " - Click to reset the scroll target window" << std::endl; MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } KillTimer(hwnd, 1); return 0; }
Summary of changes:
- Added handling of
WM_MOUSEMOVEinWindowProcto updatelockedCursorPosand unlock the cursor lock. - Removed the
SetCursorPos(lockedCursorPos.x, lockedCursorPos.y);call insideProcessTrackPointMovementto prevent cursor jumping. - On mouse clicks, reset the scroll target and unlock the cursor lock (already present).
- On starting to scroll, lock the cursor position once.
Please try this updated script. Now, when you move the cursor manually, the scroll target and locked cursor position update accordingly, and the cursor will no longer jump back when you start scrolling.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.