SDL2 window sometimes fails to gain input focus on macOS at startup
On macOS, an SDL2 window usually (75%+) gains input focus and receives mouse and keyboard input automatically at startup. However, occasionally it does not, and only receives events after being minimized and restored. In those cases, clicking the window cannot give it focus.
Platform: macOS[13.2], SDL2 [2.32.8]
SDL_Window* CreateWindow()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
return nullptr;
}
SDL_Window* window = SDL_CreateWindow(
"MyApp",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
960, 540,
SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI
);
if (!window) {
SDL_Quit();
return nullptr;
}
SDL_RaiseWindow(window);
SDL_SetWindowInputFocus(window);
return window;
}
Are you seeing this with SDL3 as well?
A few months ago, I tried SDL3 (3.2.18) but couldn’t get mouse or keyboard input to work, which is why I’m using SDL2 for now. At the time, I hadn’t tried the minimize-and-restore solution, so it might have been the same issue. Will try the latest SDL3 when I have time, though it probably won’t be very soon.
same issue on mac - SDL_WINDOW_ALWAYS_ON_TOP seemed to make it happen less often - but still does. Does that help your case a bit? (SDL2)
(Assigning to myself, but I'm not likely to get to this for SDL2 for awhile. If someone says this is happening in SDL3 also, though, it goes to the top of the queue.)
I noticed it happens when I move the mouse whilst starting the game. Possibly iTerm2 steals the focus while the game is starting.
This is possibly fixed by b2c3e6fadec4be4f16febf1b5046fe0cc950addb, which should be in 2.32.10. If anyone can upgrade and verify, I'd appreciate it.
Alas, for me I'm on 2.32.10 already. I have a workaround, check if SDL_GetKeyboardFocus is null and if so, restart the program :) Not a great workaround I'll admit. I tried quitting the SDL video/destroying the window, but on the mac these things don't seen to do anything w.r.t closing the window immediately. I'll try SDL3 at some point too
I just upgraded to SDL3. I started and stopped my application about 10 times, and I didn’t encounter this issue. I’ll comment if I manage to reproduce it again.
However, I do have two other issues in SDL3. https://github.com/libsdl-org/SDL/issues/14107 https://github.com/libsdl-org/SDL/issues/14108
same issue on mac - SDL_WINDOW_ALWAYS_ON_TOP seemed to make it happen less often - but still does. Does that help your case a bit? (SDL2)
After creating this issue, it occurs very rarely (less than 5%) in SDL2. I can’t remember whether it was caused by my code changes or by upgrading to the newest SDL2. Also, I did not use SDL_WINDOW_ALWAYS_ON_TOP.
@icculus This issue still exists in the latest SDL3. Although it doesn’t occur very often, it works correctly most of the time (over 95%).
Also seeing this issue in latest SDL3.
Just checking: if you move the mouse while launching the app, does it reproduce 100% of the time?
yes!
I have my game restart in a loop if it detects the lack of focus. I noticed if I keep moving the mouse during restart, then the issue happens over and over.
If I stop moving the mouse during startup, the issue is gone
So 100% related to mouse movement
thanks
On Tue, 7 Oct 2025 at 21:29, Ryan C. Gordon @.***> wrote:
icculus left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3378600406
Just checking: if you move the mouse while launching the app, does it reproduce 100% of the time?
— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3378600406, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3H4BFPSYX57VXL77ETL3WQPA7AVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTGNZYGYYDANBQGY . You are receiving this because you commented.Message ID: @.***>
Okay, we have something to go on here, then. Let me dig in a little deeper over here, and I'll report back.
Possibly iTerm2 steals the focus while the game is starting.
iTerm2 updated when I booted the Mac today (but this update might have been many days old, this isn't my primary development system).
Today I'm noticing that I'm always failing to bring the app to the foreground now with the latest SDL3. I'm also using iTerm2. Running examples/render-clear from iTerm2 never brings it to the foreground until I seek it out on the Dock. I double-clicked the examples/render-clear.app from the Finder, it doesn't have this problem. Launched the non-app-bundled examples/renderer-clear binary from the standard Terminal app...and it works correctly.
Are we all using iTerm2? Could this be the culprit?
(Restarting iTerm2 fixed this specific problem, fwiw.)
I'm having a similar problem with FNA + SDL3 and Rider. When i launch the app from Rider it was not automatically showing up but hiding in the dock instead.
When launching from iTerm2 the app window open behind the terminal + not having focus instead of showing on top of the terminal like normal.
This issue started to happen from SDL 3.2.18, as older versions like 3.2.16 works fine for me.
This issue started to happen from SDL 3.2.18, as older versions like 3.2.16 works fine for me.
Can you use git bisect to identify where it broke for you?
Guessing it's going to be https://github.com/libsdl-org/SDL/commit/b2c3e6fadec4be4f16febf1b5046fe0cc950addb
Guessing it's going to be b2c3e6f
yes it is!, the log points to another commit but the code this the same: 17656d051b750b945fdd8b4608f9524c7879bc1b
I don't know what it does but removing this line
background_app_default = true; /* by default, don't explicitly activate the dock and then us again to force to foreground */ in SDL_cocoaevents.m makes it focus normally on launch again.
Do we have a contact at Apple still, @slouken? It would be useful if someone could tell us why this Dock trick was necessary and why nothing else needs this.
Like, what are we doing that we don't get focused on launch by default?
Like, is this more "oh, you didn't use a .nib file?!" fallout...?
Like, is this more "oh, you didn't use a .nib file?!" fallout...?
Yeah, I think so. Launching from terminal has different finder behavior than launching from an app bundle.
I found a workaround for my purposes: after creating the window, I flush the event loop completely. Then I call SDL_RaiseWindow, and flush the event loop one more time. This consistently focuses the window on startup--hope that helps any fixes here!
Kevin, you mean?
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); SDL_RaiseWindow(sdl.window); SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
didn't work for me (SDL2)
thanks
On Sun, 9 Nov 2025 at 10:48, Kevin Watters @.***> wrote:
kevinw left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3507953632
I found a workaround for my purposes: after creating the window, I flush the event loop completely. Then I call SDL_RaiseWindow, and flush the event loop one more time. This consistently focuses the window on startup--hope that helps any fixes here!
— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3507953632, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3H2AD2Y4CV5PCGIEDO3334LY7AVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTKMBXHE2TGNRTGI . You are receiving this because you commented.Message ID: @.***>
@goblinhack not quite (and as I mentioned, I'm on SDL3) -- I just use while SDL_PollEvent(&evt) {}
That worked! (SDL2)
#if APPLE SDL_Event evt; // // Work around macos focus issue, possibly caused by iTerm // while (SDL_PollEvent(&evt)) {} #endif
SDL_RaiseWindow(sdl.window);
On Tue, 11 Nov 2025 at 11:42, Kevin Watters @.***> wrote:
kevinw left a comment (libsdl-org/SDL#13920) https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3516481474
@goblinhack https://github.com/goblinhack not quite (and as I mentioned, I'm on SDL3) -- I just use while SDL_PollEvent(&evt) {}
— Reply to this email directly, view it on GitHub https://github.com/libsdl-org/SDL/issues/13920#issuecomment-3516481474, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADA3HZ7HXSE43OBUZ2DKQ334HDTNAVCNFSM6AAAAACGE2Z2HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTKMJWGQ4DCNBXGQ . You are receiving this because you were mentioned.Message ID: @.***>
Okay, so it sounds to me like maybe SDL just needs to wait until a certain event comes through from Cocoa, sometime after applicationDidFinishLaunching, and then do some final efforts there. I'll see if I can isolate it.