-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDL2: key.keycode.sym returns a scancode instead of a keycode #21377
Comments
This this change with the recent SDL2 version bump (#21337)? i.e. can you reproduce this on emscripten versions older than 3.1.54? @Daft-Freak WDYT? |
I'd guess this is something to do with libsdl-org/SDL@9221548 as we didn't set keycodes/ Also, I think some of the tests are checking keycodes? |
Just in case this helps: for all the keys I've checked, |
BTW I tried with 3.1.53, which does not include the SDL2 version bump, and I ran into the same problem. |
I haven't managed to reproduce this, always getting the "this branch does NOT execute" message. Using this full test program: #include <stdio.h>
#include <SDL.h>
#include <emscripten.h>
void loop() {
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");
}
printf("sym %i scancode %i\n", e.key.keysym.sym, e.key.keysym.scancode);
}
}
}
int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("sdl2_key", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
emscripten_set_main_loop(loop, 0, 1);
return 0;
} (Tried a few browser/OS combinations, including Chrome + macOS) |
Oh my goodness. It turns out that my problem was in my headers: I was including Sigh. Sorry to take up everybody's time with this. I wonder if there's a way to protect against a terrible mistake like this. |
Ah, I should've noticed that your event fields were all off by one. Another issue caused by emscripten's weird "not SDL1.2, but not SDL2 either" headers 🤔 |
If you only include |
I checked again and it looks like If so I think we can close this issue? |
Would be nice if the SDL1 headers didn't declare a bunch of SDL2 functions so things like this would fail earlier... but I guess that's another issue. |
Don't SDL1 and SDl2 share a lot of the same function names? Is there something about the SDL1 headers in emscripten that are different here compared to normal SDL1 headers? |
Some parts are the same, but others like video are almost entirely different.
The headers in emscripten are from SDL1.3 (early pre-release SDL2), which defines both, but most of the new API isn't implemented in The main side-effect of this being that if you include the wrong header by accident, SDL2 code usually compiles but as the structs are outdated you get issues like this one. |
Thanks for the explanation. I guess its too late (or not worth) it to revert or SDL1 headers to true SDL1. We could at least try to trim them to stuff that we support.. but thats also outside the scope of this bug. I think we can close this one. |
(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 aSDL_Scancode
instead of aSDL_Keycode
. I would expectevent.key.keysym.sym
to return a SDL_Keycode according to the SDL documentation. Example:I'm using Emscripten 3.1.54, which uses SDL 2.26.0. I'm compiling the code with the following command:
I'm opening the generated page in Chrome Version 121.0.6167.160 on MacOS 11.7.10.
Version of emscripten/emsdk:
Full link command and output with
-v
appended:log.txt
The text was updated successfully, but these errors were encountered: