Skip to content

Instantly share code, notes, and snippets.

@v-rob
Created October 24, 2023 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v-rob/4ef28c7ae9f45fcf697dbc0f3a64ee5c to your computer and use it in GitHub Desktop.
Save v-rob/4ef28c7ae9f45fcf697dbc0f3a64ee5c to your computer and use it in GitHub Desktop.
SDL keycodes vs scancodes
// Can be compiled on Linux with the command:
// gcc -Wall -Wextra -std=c99 sdlscankey.c -o sdlscankey $(pkg-config --cflags --libs sdl2) -lm
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
bool pollEvents() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
return false;
case SDL_KEYDOWN:
printf(" keycode=%s\nscancode=%s\n\n",
SDL_GetKeyName(e.key.keysym.sym),
SDL_GetScancodeName(e.key.keysym.scancode));
break;
}
}
return true;
}
int main(void) {
if (SDL_Init(SDL_INIT_VIDEO)) {
puts(SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("Keycodes",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 300, SDL_WINDOW_SHOWN);
if (window == NULL) {
puts(SDL_GetError());
SDL_Quit();
return 1;
}
puts("Keycode: Should display the character labeled on the keyboard");
puts("Scancode: Should display the character as if it were a QWERTY keyboard");
while (pollEvents()) {
SDL_Surface *surface = SDL_GetWindowSurface(window);
if (surface == NULL) {
continue;
}
SDL_FillRect(surface, NULL, 0);
SDL_UpdateWindowSurface(window);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment