views:

22

answers:

2

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 (evt.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.

+1  A: 
private function onKeyUp(evt:KeyboardEvent):void
{
    if (evt.charCode == 'd'.charCodeAt(0) )
    {
        trace('Someone pressed the letter d');
    }
}

should do it.

djacobs7
A: 

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');
    }
}
Hanse