From 6a86c2990e4c96defc6cb8d92979102ca073f028 Mon Sep 17 00:00:00 2001 From: Brett Lawson Date: Wed, 5 Nov 2025 14:56:31 -0800 Subject: [PATCH] fix: Adjust OS X MouseMove handling to use live cursor position. --- src/lib/platform/OSXScreen.h | 2 +- src/lib/platform/OSXScreen.mm | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/lib/platform/OSXScreen.h b/src/lib/platform/OSXScreen.h index 30332ce2c..8a24bcfdb 100644 --- a/src/lib/platform/OSXScreen.h +++ b/src/lib/platform/OSXScreen.h @@ -111,7 +111,7 @@ private: void sendClipboardEvent(EventTypes type, ClipboardID id) const; // message handlers - bool onMouseMove(CGFloat mx, CGFloat my); + bool onMouseMove(); // mouse button handler. pressed is true if this is a mousedown // event, false if it is a mouseup event. macButton is the index // of the button pressed using the mac button mapping. diff --git a/src/lib/platform/OSXScreen.mm b/src/lib/platform/OSXScreen.mm index ee318cdb2..19afc2504 100644 --- a/src/lib/platform/OSXScreen.mm +++ b/src/lib/platform/OSXScreen.mm @@ -913,8 +913,17 @@ void OSXScreen::handleSystemEvent(const Event &event) } } -bool OSXScreen::onMouseMove(CGFloat mx, CGFloat my) +bool OSXScreen::onMouseMove() { + // when we receive a mouse-move event, it is possible it was queued for a period + // and that the mouse has already moved again since then. to handle this, we need + // to query the current mouse position rather than using the position in the event. + CGEventRef event = CGEventCreate(NULL); + CGPoint pos = CGEventGetLocation(event); + CFRelease(event); + CGFloat mx = pos.x; + CGFloat my = pos.y; + LOG_DEBUG2("mouse move %+f,%+f", mx, my); CGFloat x = mx - m_xCursor; @@ -1613,7 +1622,6 @@ OSXScreen::handleCGInputEventSecondary(CGEventTapProxy proxy, CGEventType type, CGEventRef OSXScreen::handleCGInputEvent(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { OSXScreen *screen = (OSXScreen *)refcon; - CGPoint pos; switch (type) { case kCGEventLeftMouseDown: @@ -1630,8 +1638,9 @@ CGEventRef OSXScreen::handleCGInputEvent(CGEventTapProxy proxy, CGEventType type case kCGEventRightMouseDragged: case kCGEventOtherMouseDragged: case kCGEventMouseMoved: - pos = CGEventGetLocation(event); - screen->onMouseMove(pos.x, pos.y); + // we intentionally ignore the position in the event here as the events are + // queued and will no longer be accurate when we process them. + screen->onMouseMove(); // The system ignores our cursor-centering calls if // we don't return the event. This should be harmless,