Understanding Keyboard State

Determining a Key's Scan Code

How do I determine the scan code for a specific key?

Abstract art representing computer programming

In SDL, scan codes are used to represent the physical location of keys on a keyboard. These codes are independent of the keyboard layout, making them useful for handling input in a consistent manner across different systems and languages.

Using SDL’s Scan Code Definitions

SDL provides a set of predefined scan codes in the SDL_scancode.h header file. These scan codes are named constants that represent each key on the keyboard. For example:

  • SDL_SCANCODE_A represents the 'A' key.
  • SDL_SCANCODE_SPACE represents the space bar.
  • SDL_SCANCODE_UP represents the up arrow key.

Example

Here’s how you can use these scan codes in your code:

#include <SDL.h>
#include <iostream>

class Window {
 public:
  Window() {
    SDL_Init(SDL_INIT_VIDEO);
    SDLWindow = SDL_CreateWindow(
      "Scan Codes",
      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";
    }
    if (state[SDL_SCANCODE_SPACE]) {
      std::cout << "Space key is pressed\n";
    }
    if (state[SDL_SCANCODE_UP]) {
      std::cout << "Up arrow key is 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 pressed
Space key is pressed
Up arrow key is pressed

Finding Scan Codes

If you need to find the scan code for a specific key, you can refer to the SDL_scancode.h file on SDL’s GitHub repository. This file lists all the scan codes supported by SDL.

Custom Keys

If you are working with custom hardware or unusual keys, you might need to determine the scan code programmatically.

Here’s a simple way to print the scan code of any key pressed:

#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window = SDL_CreateWindow(
    "Find Scan Codes",
    SDL_WINDOWPOS_CENTERED,
    SDL_WINDOWPOS_CENTERED,
    640, 480, SDL_WINDOW_SHOWN
  );

  SDL_Event event;
  bool running = true;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_KEYDOWN) {
        std::cout << "Scan code: "
          << event.key.keysym.scancode << '\n';
      }
    }
    // Rendering code here
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Scan code: 4 // When 'A' key is pressed
Scan code: 44 // When 'SPACE' key is pressed

Conclusion

Using SDL’s predefined scan codes is the most straightforward way to handle key inputs consistently across different keyboard layouts.

For special cases, you can refer to SDL’s documentation or determine scan codes programmatically.

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:

  • 67 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