emscripten-core/emscripten

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

Open

#20,334 创建于 2023年9月25日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)C++ (27,361 star) (3,519 fork)batch import
help wanted

描述

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

贡献者指南