Understanding Keyboard State

Keyboard State When Application Loses Focus

What happens to the keyboard state array when my application loses focus?

Abstract art representing computer programming

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.

Behavior on Focus Loss

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.

Example

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

Why This Matters

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.

Detecting Focus Changes

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

Conclusion

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.

This Question is from the Lesson:

Understanding Keyboard State

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.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Understanding Keyboard State

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.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 36 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved