In actionscript, how do I write keyboard handler without typing ascii codes in my code?
I am writing a keyboard event handler in actionscript. I would like to trace something when the letter "d" is pressed.
private static const THE_LETTER_D:int = 100;
private function onKeyUp(evt:KeyboardEvent):void
{
if (e开发者_StackOverflowvt.keyCode == THE_LETTER_D )
{
trace('Someone pressed the letter d');
}
}
Is there a way I can do this without defining THE_LETTER_D? I tried to do int('d') but that does not work, and I am not sure what else to try.
private function onKeyUp(evt:KeyboardEvent):void
{
if (evt.charCode == 'd'.charCodeAt(0) )
{
trace('Someone pressed the letter d');
}
}
should do it.
The flash.ui.Keyboard component holds a couple of constants that represents keyboard characters.
private function onKeyUp(evt:KeyboardEvent):void
{
if (evt.charCode == Keyboard.D)
{
trace('Someone pressed the letter D');
}
}
精彩评论