How can I get the keyboard layout?

Hi!

I try to detect the keyboard layout (QWERTY, QWERTZ, AZERTY, DVORAK, etc…). I’m on Linux with an AZERTY keyboard and when I press the key “1” on the top of the keyboard (not the one on the numerical pad on the right side), the key code returned by KeyEvent.getKeyCode() is never VK_1, it returns VK_AMPERSAND. On a QWERTY keyboard, VK_AMPERSAND is on the same key than 7. I would like to use these keys as direct shortcuts to select a particular weapon like in Doom. JInput does not give a better result except if I press SHIFT and “1”. I know getExtendedKeyCode might be useful but it is only in Java 1.7. I could use the InputContext but I’m not sure a Locale is enough to know which keyboard layout is used.

Do you know a way of getting the keyboard layout?

Can’t you use getKeyChar() for weapon selection?

No I can’t because the char returned for the virtual keys VK_NUMPAD1 and VK_1 is the same whereas the num pad is used to move and the numerical keys are used to choose a weapon. Knowing the keyboard layout would be useful to choose a nice equivalent to WSAD for each layout.

If this is for personal use? If so then you could just have it ask you to press a stated key at startup (like 1). Have the layouts hard coded in and pick one based on the event it receives.

Alternatively you could build some scripts (bash, VBScript, etc) for getting the keyboard layout and then have your app just run that script. It’s not elegant but it should work.

Not that it wouldn’t be a nice feature, but I don’t think AAA FPS do anything special. Just allowing customization is enough.

Ok it is possible to make a difference between the key that should use the code VK_1 and the key that uses the code VK_NUMPAD1 by using getKeyLocation() and the default Locale instance seems to contain almost the same thing than $LANG so it might be enough. My fix is there (not very elegant):
http://www.ardor3d.com/forums/viewtopic.php?f=10&t=999&p=7639#p7635

Hi!

What returns KeyEvent.paramString() on Windows and Mac? On Linux, the scan code that should appear in this string is always zero and I don’t know what is the raw code.

[quote]Input methods are unavailable if (a) no input method written in the Java programming language has been installed and (b) the Java 2 implementation or the underlying platform does not support native input methods. In this case, input contexts can still be created and used; their behavior is specified with the individual methods below.
[/quote]
That is impressively on topic spam right there (his signature has links).

Had. :wink:
It’s a copy and paste from InputContext javadocs.