When your SDL application loses focus, the keyboard state array will reflect that no keys are pressed. This behavior ensures that your application does not continue to process key presses while it is not the active window.
SDL handles input focus by updating the keyboard state array to all zeros when the application loses focus.
This means every position in the array will be set to 0
, indicating that no key is pressed. This is important for maintaining correct input behavior in your application.
Here’s a simple example demonstrating this behavior:
#include <SDL.h>
#include <iostream>
class Window {
public:
Window() {
SDL_Init(SDL_INIT_VIDEO);
SDLWindow = SDL_CreateWindow(
"Focus Test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN
);
}
~Window() {
SDL_DestroyWindow(SDLWindow);
SDL_Quit();
}
void HandleKeyboard() {
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_A]) {
std::cout << "A key is pressed\n";
} else {
std::cout << "A key is not pressed\n";
}
}
private:
SDL_Window* SDLWindow{nullptr};
};
int main(int argc, char* argv[]) {
Window GameWindow;
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
GameWindow.HandleKeyboard();
// Rendering code here
}
return 0;
}
A key is not pressed
A key is pressed
A key is not pressed
When the application window loses focus, you don’t want it to continue processing inputs as if it were still active.
For example, if the user is playing a game and switches to another window, the game should not keep moving the character or performing actions based on the last key state.
If you need to handle specific actions when your application gains or loses focus, you can listen for SDL_WINDOWEVENT
events. Here’s how:
#include <SDL.h>
#include <iostream>
class Window {
public:
Window() {
SDL_Init(SDL_INIT_VIDEO);
SDLWindow = SDL_CreateWindow(
"Focus Event",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN
);
}
~Window() {
SDL_DestroyWindow(SDLWindow);
SDL_Quit();
}
void HandleEvent(const SDL_Event& event) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event
== SDL_WINDOWEVENT_FOCUS_GAINED) {
std::cout << "Window gained focus\n";
} else if (event.window.event
== SDL_WINDOWEVENT_FOCUS_LOST) {
std::cout << "Window lost focus\n";
}
}
}
private:
SDL_Window* SDLWindow{nullptr};
};
int main(int argc, char* argv[]) {
Window GameWindow;
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
} else {
GameWindow.HandleEvent(event);
}
}
// Rendering code here
}
return 0;
}
Window gained focus
Window lost focus
When your SDL application loses focus, the keyboard state array is set to indicate that no keys are pressed.
This prevents your application from processing stale input. Additionally, you can handle focus changes explicitly using SDL_WINDOWEVENT
 events.
Answers to questions are automatically generated and may not have been reviewed.
Learn how to detect and handle keyboard input in SDL2 using both event-driven and polling methods. This lesson covers obtaining and interpreting the keyboard state array.