Skip to content

Fix SDL3 focus handling: restore mouse grab and cursor state on alt-tab/Win key#2856

Merged
riccardobl merged 2 commits into
masterfrom
copilot/investigate-focus-issue-sdl3
Jun 8, 2026
Merged

Fix SDL3 focus handling: restore mouse grab and cursor state on alt-tab/Win key#2856
riccardobl merged 2 commits into
masterfrom
copilot/investigate-focus-issue-sdl3

Conversation

Copilot AI commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

On Windows with SDL3, alt-tabbing or pressing the Win key leaves the cursor invisible on the desktop and breaks camera rotation when clicking back into the app.

Root causes in SdlMouseInput:

  • SDL_HideCursor() is never reversed on focus loss → cursor invisible everywhere outside the app
  • SDL_SetWindowRelativeMouseMode + mouse grab are released by SDL3 on focus loss but never re-applied on focus regain → mouse delta input stops working

Fix: Handle SDL_EVENT_WINDOW_FOCUS_LOST and SDL_EVENT_WINDOW_FOCUS_GAINED in SdlMouseInput.onSDLEvent():

  • Focus lost: explicitly release capture/grab/relative mode; call SDL_ShowCursor() if cursor was hidden; reset X11 warp state to avoid stale warp events
  • Focus gained: call setCursorVisible(cursorVisible) to re-apply grab, relative mode, and cursor visibility as they were before focus was lost
if (type == SDL_EVENT_WINDOW_FOCUS_GAINED) {
    if (event.window().windowID() == context.getWindowId()) {
        setCursorVisible(cursorVisible);  // re-enables grab + relative mode
    }
    return;
}
if (type == SDL_EVENT_WINDOW_FOCUS_LOST) {
    if (event.window().windowID() == context.getWindowId()) {
        x11WarpGrabMode = false;
        ignoreNextX11WarpEvent = false;
        SDL_CaptureMouse(false);
        SDL_SetWindowMouseGrab(context.getWindowHandle(), false);
        SDL_SetWindowRelativeMouseMode(context.getWindowHandle(), false);
        if (!cursorVisible) SDL_ShowCursor();
    }
    return;
}

Copilot AI changed the title [WIP] Investigate issue with regaining focus on SDL3 in Windows Fix SDL3 focus handling: restore mouse grab and cursor state on alt-tab/Win key Jun 8, 2026
Copilot AI requested a review from riccardobl June 8, 2026 08:06
@riccardobl riccardobl marked this pull request as ready for review June 8, 2026 08:53
@riccardobl riccardobl added this to the v3.10.0 milestone Jun 8, 2026
@riccardobl riccardobl merged commit c29eedb into master Jun 8, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trouble regaining focus properly with SDL3 on windows

2 participants