emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.63k stars 3.28k forks source link

`glfwSetKeyCallback()` : inconsistent behavior on non QWERTY keyboard layouts #20334

Open SuperUserNameMan opened 11 months ago

SuperUserNameMan commented 11 months ago

This is going to be complicated to explain, so please, be patient with me :

My keyboard is AZERTY layout.

My A key is located at the same location than the Q key of a QWERTY keyboard. At the hardware level, they share the same scancode, because : same location = same scancode.

Using keyboard scancodes is helpful when you make a video-game that maps "WASD" keys to directions, because the location of these "WASD" keys are always the same whatever is the layout of the user's keyboard. The game can work out of the box without remapping the keyboard.

Here is the issue :

When I use GLFW in C, the callback passed to glfwSetKeyCallback() will receive correct scancodes. When I use GLFW in Emscripten, the callback passed to glfwSetKeyCallback() will receive INCORRECT scancodes.

This is because library_glfw.js makes use of the outdated KeyboardEvent.keyCode instead of KeyboardEvent.code.

.keyCode is not an hardware scancode, and it breaks compatibility and consistency between GLFW in C and Emscripten.

https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/src/library_glfw.js#L410-L433

https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/src/library_glfw.js#L379-L382

https://github.com/emscripten-core/emscripten/blob/9bdb310b89472a0f4d64f36e4a79273d8dc7fa98/src/library_glfw.js#L149-L161

sbc100 commented 11 months ago

Sounds like this is a straightforward bug in our GLFW. Do you think there is any risk to simply switching from keyCode to just `code? Would you be interesting in sending a PR to that effect? I imagine to could be hard to test such as change :(

SuperUserNameMan commented 11 months ago

Do you think there is any risk to simply switching from keyCode to just code ?

Alas, keyCode returns an integer, and code returns a string.

https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values

Would you be interesting in sending a PR to that effect?

Alas (again), I'm not good enough with the javascript universe, and i'm a total noob with emscripten. Plus, according to this page, there seems to be some browser and OS variations that I might not be competent to handle correctly.

I imagine to could be hard to test such as change :(

I think that changing the keyboard layout at OS level could help with testing. And if the OS has a virtual keyboard, it might help too.