sunnuntai 20. lokakuuta 2019

Keyboard layout issues

Bit of mild rant, bit of memory lane...

Back in the 90s, when I first got into flight simulators, I learned the hard way that I had to switch the computer to US keyboard layout to even play the games. This was because Finnish keyboard layout has keys 1-0 and + and ´ on top row (excluding whatever is the leftmost key, and backspace), while games expected US layout where keys there are 1-0, - and =. On Microprose simulators and many others these keys were typically used for throttle controls, so without them even take off was impossible. Between different keyboard layouts only A-Z and 1-0 are common between layouts (completely ignoring non-latin alphabets, and even within latin charset there are exceptions like AZERTY layout), and pretty much all other characters move around when switching between them.

I recently bought Stardew Valley and found it surprisingly fun game. It did, however, bring me back few decades due to this very same keyboard issue I though long since solved.

Why this came into my mind is that Stardew Valley has keyboard shortcuts, 12 of them to be exact, for quick access to items. Short cut keys being 1-0, - and =, to be specific. So, the abovementioned US layout. And of course it doesn't work properly with Finnish layout, not with the actual keys, nor by using, say, - character (which happens to be in bottom row in this layout). So I can't use two rightmost items with keyboard. And here I thought we had left these issues behind some 20 years ago.

Funnily enough, it shouldn't be that difficult to make game keyboard universal. Almost all keyboards have operated, and as far as I know, still operate only on "scan codes" that indicate physical position of a key on keyboard that was pressed or released, and translation to actual characters doesn't happen until your operating system gets involved. Keyboards are all standard these days regarding normal keys, and only vendor-specific keys do require specific driver to translate them to useful function.

Last time I actually dealt with the scan codes was some 20 years ago so my memory may be a bit hazy here, but I seem to recall that it mostly went top to bottom and left to right; ESC being 1, F1 2, then proceeding to I think F10 (F11 and F12 being added later to sequence and therefore had different codes) before proceeding to next row; the leftmost key, 1 onward until backspace, then tab... And keypad portion later on sequence.

Operating system (or more specifically, keyboard driver) typically takes care of translating these codes to actual characters so often you don't have to even know about them on application level. Only when building things like Stardew Valley's 12-item shortcut bar you'd want to take different route to avoid the layout issues like this.

Most, if not all common APIs do report keyboard scan codes as well on key press, so all game would have to do here is to track them instead of characters to make your game work regardless of user's keyboard layout.

The other part, showing user the key "name", I haven't ever used, but I am certain that somewhere there is a application-level function to translate scan code to key name too, just for this purpose. And voilá, no more keyboard layout issues!

Now I just have to wonder, if user isn't using QWERTY layout (but something else like AZERTY), do character movement break too, since game uses ASDW for directional movement? I'd hope not.



Ei kommentteja:

Lähetä kommentti