Skip to content

SDL2: key.keycode.sym returns a scancode instead of a keycode #21377

@giogadi

Description

@giogadi

(I also posted this issue on the SDL repo here)

When I inspect the keycode associated with a keydown event using event.key.keysym.sym, the value returned is a SDL_Scancode instead of a SDL_Keycode. I would expect event.key.keysym.sym to return a SDL_Keycode according to the SDL documentation. Example:

SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
    if (e.type == SDL_KEYDOWN) {
        if (e.key.keysym.sym == SDLK_a) {
            printf("When I press the A key on my keyboard, this branch does NOT execute\n");
        } else if (e.key.keysym.sym == SDL_SCANCODE_A) {
            printf("This branch DOES execute when I press the A key on my keyboard.\n");
        }
    }
}

I'm using Emscripten 3.1.54, which uses SDL 2.26.0. I'm compiling the code with the following command:

emcc main.c -o index.html -sUSE_SDL=2

I'm opening the generated page in Chrome Version 121.0.6167.160 on MacOS 11.7.10.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.54 (a95c44ee924d02fa1498f846595485d27c31daa8)
clang version 19.0.0git (https://github.com/llvm/llvm-project e769fb8699e3fa8e40623764f7713bfc783b0330)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/luis/code/emsdk/upstream/bin

Full link command and output with -v appended:
log.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions