KeyboardEvent.keyCode shows wrong values on cpp (windows)

The following code shows what I mean. When I compile it to cpp on windows and press different key (for example "A" and "NUMPAD_ONE") it traces same keyCode values. Or try to press letter keys. For you information: keyCodes for letter keys should be betwe…

  • Forums
  • »
  • Bugs
  • »
  • KeyboardEvent.keyCode shows wrong values on cpp (windows)
Viewing 1 to 9 (9 Total)
KeyboardEvent.keyCode shows wrong values on cpp (windows)

Zaphod

Zaphod
Total Posts: 218
Joined: September 15, 2011

The following code shows what I mean. When I compile it to cpp on windows and press different key (for example "A" and "NUMPAD_ONE") it traces same keyCode values.
Or try to press letter keys. For you information: keyCodes for letter keys should be between 65 and 90. But in cpp on windows it returns values that are more than 90.

This is very annoying.

package;

import nme.events.KeyboardEvent;
import nme.Lib;
import nme.display.Sprite;
import nme.ui.Keyboard;


class Sample extends Sprite
{
public function new()
{
super();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}

function reportKeyDown(event:KeyboardEvent)
{
trace("Key Pressed: " + String.fromCharCode(event.charCode) + " (key code: " + event.keyCode + " character code: " + event.charCode + ")");
}

public static function main()
{
new Sample();
}

}

Tags:
Posted on December 15, 2011 at 3:42 AM

Chman

Chman
Total Posts: 51
Joined: September 12, 2011

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

I've noticed this as well, but I've made myself a small "Key" singleton so I don't have to worry about it anymore.

See, for example, the Key class in Haxepunk : https://github.com/MattTuttle/HaxePunk/blob/master/src/com/haxepunk/utils/Key.hx

(Although it doesn't fix the numpad keys from being the same as the normal keys).

Posted on December 15, 2011 at 2:43 PM

Zaphod

Zaphod
Total Posts: 218
Joined: September 15, 2011

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

Thank you very much for this workaround. I'll steal it to my flixel port smiling

Posted on December 15, 2011 at 2:48 PM

aholla

aholla
Total Posts: 31
Joined: November 03, 2011

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

HI, I have also just run into this. I really think NME should standardise this. Flash and JS targets are the same but CPP is different. I think NME should handle the conversion of the FLASH keycodes to the CPP keycodes unless there is something I am missing.

Posted on January 12, 2012 at 5:25 AM

singmajesty

singmajesty
Total Posts: 2141
Joined: August 25, 2011

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

I just dug into this, and found the cause of the difference. It seemed obvious when seeing the Key class, but I've applied the actual fix to my KeyBinding library. I apologize that it was not binding properly for alphabetical keys in C++ and Neko.

Very simple -- when you press a letter of the alphabet, Flash reports the uppercase key code. C++ and Neko report the lowercase key code.

I don't think we should get into manipulating these platforms to report the same codes as Flash, because whatever that "fix" is, it will likely come back as a terrible curse in the future.

public static function getKeyCode (key:String):Int {

#if (cpp || neko)

return key.toLowerCase ().charCodeAt (0);

#else

return key.toUpperCase ().charCodeAt (0);

#end

}

Posted on January 12, 2012 at 10:43 AM

OhiraKyou

OhiraKyou
Total Posts: 3
Joined: August 19, 2012

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

Unfortunately, this issue is still relevant, a year later. I absolutely need numpad and alphabet key support in Windows. Has a year really yielded no solution? Key class: https://github.com/MattTuttle/HaxePunk/blob/master/src/com/haxepunk/utils/Key.hx

Posted on August 19, 2012 at 8:41 PM

cambiata

cambiata
Total Posts: 22
Joined: November 04, 2011

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

Here's what I've come up with. If neko or cpp targets, it corrects the keyCodes for keys a-z, while the numpad keys remain unaltered. Maybe useful to someone:

static public function getKeyCode(e:KeyboardEvent) {
var keyCode = e.keyCode;
//trace([e.keyCode, e.charCode]);
#if (neko || cpp)
if (e.keyCode == e.charCode)
if (keyCode >= 65 && keyCode <= 122) {
keyCode = keyCode-32;
}
#end
return keyCode;
}

EDIT:

Here's a gist on this workaround: https://gist.github.com/3673008

Posted on September 06, 2012 at 1:15 AM

dcfedor

dcfedor
Total Posts: 2
Joined: November 09, 2012

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

Hey Guys,

I'm just getting my feet wet with HaxeNME, so I apologize if I'm missing anything obvious.

I struggled with this issue a bit myself, and tried a few different solutions, including vanilla KeyboardEvent.keyCode, the KeyBinding haxelib, and the Key.hx class linked above.

However, I noticed that the KeyboardEvent.keyCode generated for KEY_DOWN and KEY_UP events is different on the cpp target. For example, if I press the "W" key, I get two events:

Key down: 119[w]
Key up: 87[W]

The ints above are the event.keyCodes. Is this a bug?

I think what singmajesty says above about not changing architecture is good advice. However, if the "down" and "up" event being generated by the same key are different, it seems clear that one of them is incorrect. It would be cool if the incorrect one was the lowercase code, since that would make both platforms play nice again. I'm not yet well-versed enough to dig into the code, though.

Any thoughts?

Posted on November 09, 2012 at 12:18 PM

dcfedor

dcfedor
Total Posts: 2
Joined: November 09, 2012

Re: KeyboardEvent.keyCode shows wrong values on cpp (windows)

Hey Guys,

Just a follow-up, in case anyone else is running into this. The version of Haxe NME that produced my above issues (mis-reporting of keys, such as W down != W up) was 3.2.0. However, downloading 3.4.4 beta seems to have corrected the problem. Additionally, mis-reported keys in the TextField (' # $ etc) object now appear to be working correctly.

Hope this helps. And thanks for the NME fixes, guys!

Posted on November 13, 2012 at 12:57 PM