From 92eaa34277f0711696d366742735fe2a2c974123 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 20 Nov 2025 13:51:23 -0800
Subject: [PATCH] cocoa: release any mouse buttons not pressed when gaining
focus
Fixes https://github.com/libsdl-org/SDL/issues/13134
---
src/video/cocoa/SDL_cocoamouse.m | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index 21bae3507e61a..8e5af2ff1c076 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -491,6 +491,22 @@ static void Cocoa_HandleTitleButtonEvent(SDL_VideoDevice *_this, NSEvent *event)
return Cocoa_MouseFocus;
}
+static void Cocoa_ReconcileButtonState(NSEvent *event)
+{
+ // Send mouse up events for any buttons that are no longer pressed
+ Uint32 buttons = SDL_GetMouseState(NULL, NULL);
+ if (buttons && ![NSEvent pressedMouseButtons]) {
+ Uint8 button = SDL_BUTTON_LEFT;
+ while (buttons) {
+ if (buttons & 0x01) {
+ SDL_SendMouseButton(Cocoa_GetEventTimestamp([event timestamp]), SDL_GetMouseFocus(), SDL_GLOBAL_MOUSE_ID, button, false);
+ }
+ ++button;
+ buttons >>= 1;
+ }
+ }
+}
+
void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
{
SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
@@ -515,6 +531,7 @@ void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
} else {
if ([event window] != NULL) {
Cocoa_MouseFocus = [event window];
+ Cocoa_ReconcileButtonState(event);
}
}