1
\$\begingroup\$

I've recently encountered a few headaches when I tried to detect key presses for a custom game engine to supports Windows, MacOS, Linux, and WebGL.

Naive approach:

At first I naively thought that using GLFW's keycodes instead of scancodes will be the best solution to cover as many operating systems as possible. I later found out that if the player has a different keyboard layout, the keys will be bound all over the place instead of having the WASD keys (regardless of what character they represent) sit together.

Problem #1

So then I moved over to using scancodes. So this works great (other than having a different set of scancodes for each operating system) but it also introduces my first issue. Since scancodes are position based and defined as integers, how will I display to the player what character, for their unique keyboard layout, is currently bound in the game's "Keyboard Settings" dialog. I don't imagine detecting a keyboard layout is plausible (might even be impossible with Javascript since I don't have access to OS calls).

Problem #2

My second issue is when the game is deployed with WebGL. In Javascript the keyCode value is just fed to the browser from the underlying operating system. What this means is I have to check the OS at runtime and remap all the keybinds based on the operating system. This is a nightmare because, if the user has customised keybinds (stored to his cloud profile) that was created on MacOS but then later opened up on Windows, then the code will have to do three remaps in order to, get the OS when keys where customised, then get the current OS, and then the conversion between the two prior to taking into consideration the customised remaps... Is there not a better way?! This would also mean I will have to send all the keycode constants for everything possible operating system to each user. Resulting in a lot of unnecessary boat in the Javascript file.

Just assume?

It seems like the big game engines out there have this issue solved and I am curious how they got around these issues. Do they just assume all players use QWERTY keyboards at the risk possibly frustrating a certain audience until they remap the keys ingame? And do they assume all browser based games are played on Windows, MacOS, and Linux to keep the javascript OS detection to a minimal? (effectively losing mobile players)

\$\endgroup\$
  • \$\begingroup\$ It seems that my following statement was incorrect: In Javascript the keyCode value is just fed to the browser from the underlying operating system. What this means is I have to check the OS at runtime ... Source: unixpapa.com/js/key.html \$\endgroup\$ – Fanus du Toit Sep 1 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.