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;
}