Skip to main content
Participant
August 15, 2013
Answered

Keyboard Function

  • August 15, 2013
  • 1 reply
  • 526 views

So I designed this code for arrow down

var to=0

stage.addEventListener(KeyboardEvent.KEY_UP, fl_KeyboardDownHandler_2);

function fl_KeyboardDownHandler_2(event:KeyboardEvent):void

{

to=1 ; gotoAndPlay(6)

}

My question, How do I modify this code so I can gain input from Alphabet. Because I tried

var to=0

stage.addEventListener(KeyboardEvent.KEY_A, fl_KeyboardDownHandler_2);

function fl_KeyboardDownHandler_2(event:KeyboardEvent):void

{

to=1 ; gotoAndPlay(6)

}

But It Doesn't Works

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Ned Murphy

If you look up the KeyboardEvent class you can see the list of constants that it supports.  KEY_A (et al) are not.

What you need to do is check the character code of the key that was pressed to determine if action is to be taken.

stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);

function checkKey(evt:KeyboardEvent):void {
      trace(evt.charCode) // use this to find the codes - comment out normally

      if(evt.charCode == 97){  // for the letter A

            to=1 ;

            gotoAndPlay(6);

      }
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
August 15, 2013

If you look up the KeyboardEvent class you can see the list of constants that it supports.  KEY_A (et al) are not.

What you need to do is check the character code of the key that was pressed to determine if action is to be taken.

stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);

function checkKey(evt:KeyboardEvent):void {
      trace(evt.charCode) // use this to find the codes - comment out normally

      if(evt.charCode == 97){  // for the letter A

            to=1 ;

            gotoAndPlay(6);

      }
}