djacobs7
djacobs7

Reputation: 11837

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

Upvotes: 1

Views: 192

Answers (2)

hanse
hanse

Reputation: 1126

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

Upvotes: 0

djacobs7
djacobs7

Reputation: 11837

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

should do it.

Upvotes: 1

Related Questions