Hi there,
I am currently working on an interactive visualization of a typewriter. To make the typing look realistic, I need information about the state of the keys on the keyboard (key is pressed, key is released).
In Unity, as I understand, such things seem to be done with either (for example)
Input.GetKeyDown(KeyCode.LESS)
or, in OnGUI(), with
if((Event.current.type == EventType.KeyDown) &&
(Event.current.keyCode == KeyCode.LESS))
//do things
and their equivalent KeyUp methods. Because things did not do what they should, I looked for what KeyCodes are sent:
if(Event.current.type == EventType.KeyDown)
Debug.Log(Event.current.keyCode)
And I see strange things happening. I am in Germany, my keyboard layout is set to german, nothing is changed from standard settings and works well in the whole (german) Windows system. Keys like `A,S,D,2` do what they should do and print `A,S,D,Alpha2`.
But keys like <,,#,+ print definitely NOT the keyCodes they should - for example, <,#, all print "BACKSLASH".
If I print Event.current.character, everything is fine ("<" prints "<"), but there seem to be localization problems with the keyCodes. I have tested this with different machines with both Unity2.5 and Unity2.6, and the problem persists. Is this a bug?
Is there any way I can get proper key-Events inside Unity without using this Method? Apart from the problems listed here, I would also need a keyCode for the key, but this one isn't even there in Unity (http://panther.unity3d.com/support/documentation/ScriptReference/KeyCode.html).
If there is no other way, I will probably have to write something like a keyHooker DLL, but that would be really not the "way it should be".
Any help would be greatly appreciated.
The [KeyCode documentation][1] says that key codes "map directly to a physical key" -- so I don't understand why we get different codes when keyboard language settings change? I can confirm that it happens, but to me it seems like a bug. (It's now three years after the original post, and I'm using Unity 3.5.6.) [1]: http://docs.unity3d.com/Documentation/ScriptReference/KeyCode.html
– yoyo