Skip to content

Instantly share code, notes, and snippets.

@masatoi
Created August 21, 2023 16:59
Show Gist options
  • Save masatoi/eba0af8492ad4141cb3147f76056b9e6 to your computer and use it in GitHub Desktop.
Save masatoi/eba0af8492ad4141cb3147f76056b9e6 to your computer and use it in GitHub Desktop.
Output Key Event tool for SDL2
/*
sudo apt install libsdl2-dev
gcc keyevent.c -o keyevent -lSDL2
*/
#include <SDL2/SDL.h>
#include <stdbool.h>
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Key Event Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} else if (event.type == SDL_KEYDOWN) {
printf("Key pressed:\n");
printf(" Name: %s\n", SDL_GetKeyName(event.key.keysym.sym));
printf(" Scancode: %d\n", event.key.keysym.scancode);
printf(" Virtual Keycode: %d\n", event.key.keysym.sym);
printf(" Modifiers: 0x%X\n", event.key.keysym.mod);
}
}
}
SDL_DestroyRenderer(renderer);
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