Saturday, August 20, 2016

How can I handle different keyboard layouts?


Let's say I have a game that uses WASD controls on a QWERTY layout. Trying to use these controls on, say, a Dvorak layout is not ideal (equivalent to on QWERTY). Obviously, I'd want to use the same physical keys as QWERTY would use (,aoe on DVORAK).



I've come up with a few possible solutions:



  • Force the user to use QWERTY

    • obviously not ideal, especially for international users



  • Change shortcuts based on keyboard layout (WASD -> ,aoe)

    • forces me to make layout maps for each supported layout (automatable)


    • easiest for the user if there are more shortcuts than just WASD



  • Force the user to define shortcuts themselves

    • More flexible

    • Annoying if there are lots of shortcuts

    • Can be used in conjunction with the second option




  • Use hardware keycodes

    • consistent across keyboards?




How is this type of thing usually handled?



Answer



Listen for scan codes. How this is done depends on your OS, which you did not list. On Windows, you can get the scancode for a given virtual key code from WM_KEYDOWN and friends by using MapVirtualKey. Scan codes are based on the physical key and are unaffected by layout.


Have a quick read of http://www.altdevblogaday.com/2011/10/02/i-never-managed-to-go-left-on-first-try/.



So yes, as Nicol Bolas said, you can and should let users work around it. But just making it work right out of the box is not hard and your users will appreciate it.


Note that you are (probably) handling character input wrong, like the vast majority of game developers. Make sure that for text, you always use WM_CHAR (or the equivalent on other OSes) rather than using WM_KEYDOWN for text input. It is completely wrong to assume that when the 'a' key is pressed that you should input an 'a' to a text input control, due to the use of Dead Keys on some layouts. You can also support IME (or OS equivalent) for East Asian markets which have far more characters than can reasonably fit on a keyboard, and need special UI to type. Handling IME in a game is a pain (handling it at all is a pain), but worth it in my opinion to increase the appeal of your product to a much, much larger set of markets. Again, WM_KEYDOWN is never to be used for text, ever.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...