Hacker News new | ask | show | jobs
by setr 1895 days ago
> For instance, the most annoying are games which default to WASD based on the letter typed and not the physical keys

Is it even possible to do anything else? I can’t imagine keyboards report their physical layout..

8 comments

Physical layout is mostly irrelevant, different keycap label arrangements use the same keycodes - the input system in your OS or application uses a keyboard layout to translate those into what you want to type.

Windows, macOS and the various input libraries on Linux provide ways to query the key map - and a number of games do use this to provide localized bindings (Ubisoft and Blizzard are notorious for doing this right, something I appreciate as a dvorak user who hates dealing with rebinding or constantly having to toggle layouts). As far as the browser there is coming support for reading the keyboard layout as well: https://developer.mozilla.org/en-US/docs/Web/API/Keyboard/ge...

Normally, keyboards report data to the computer which represents the location of a key on a standard keyboard. The OS translates to characters based on configured layout. The actual letters printed on the key caps are just decorative.

If you type WASD on a US layout USB keyboard, the computer will receive key codes 26, 4, 22, 7.

If you type ZQSD on a French keyboard you get exactly the same result.

So if you are writing a game, you might listen for codes 26, 4, 22, 7 and these will be in the sample place on the keyboard regardless of layout. You can ask the OS to translate these codes to letters for help text.

(Some of the details here are omitted—the codes above are USB HID codes, but those go through an additional OS-specific translation table before they are sent to your program.)

The only possibility is to allow the user to assign custom keys, which is pretty common.

It’s impossible to know the physical layout for absolute certainty. For all you know, my “Enter” could be a pedal under the desk.

In X11 with the Xkb extension you can configure simplified vector drawings of your input devices, which any application can query. It is intended that applications use these drawings in their help systems and tutorials, as well as in their key binding editors. Few applications bother though.

https://www.x.org/releases/current/doc/libX11/XKB/xkblib.htm...

Starcraft II had a grid (I don't remember the actual name) mode keyboard layout that worked perfectly with Dvorak without having to remap keys.

Other games like the Java version of minecraft didn't work with Dvorak when I tried them. Even with key remapping.

Scancodes from PS/2 keyboards are layout-based.
A few games map based on the key codes. For example, SC2 will get grid layout right on Dvorak or Colemak without any remapping. It did take me directly emailing a designer, who passed it on to the developers, though. ;)
Yes, the UI Events ‘code’ field represents the physical key (essentially 1:1 with USB hardware codes). The key that's labelled ‘W’ on a US keyboard and ‘Z’ on a French keyboard and ‘Ц’ on a Russian keyboard and ‘て’ on a Japanese keyboard (and so on) will have code=="KeyW" in all cases.
> Is it even possible to do anything else? I can’t imagine keyboards report their physical layout..

You can easily ask the OS what layout is being used or even ask the user.