Determining a Key's Scan Code

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

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.

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.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

SDL_PollEvent() vs SDL_PumpEvents()
What is the difference between SDL_PollEvent() and SDL_PumpEvents()?
Handling Multiple Key Presses
How can I handle multiple key presses simultaneously using SDL_GetKeyboardState()?
Advantages of Event-driven Input
What are the advantages of using event-driven input handling over polling?
Modifying the Keyboard State Array
Can I modify the keyboard state array directly?
Keyboard State When Application Loses Focus
What happens to the keyboard state array when my application loses focus?
Handling Key Releases with Polling
How can I handle key releases using the polling method?
Debouncing Key Presses in SDL
What is the best way to debounce key presses in SDL?
Detecting Multiple Keys in a Specific Order
How can I detect if multiple keys are held down in a specific order?
Polling for Continuous Interactions
What are some examples of continuous interactions best handled by polling?
Implications of Using SDL_PumpEvents() without Processing Events
What are the implications of using SDL_PumpEvents() without processing the events?
Or Ask your Own Question
Purchase the course to ask your own questions