Skip to main content
Known Participant
February 1, 2013
Question

Registering a command to a key

  • February 1, 2013
  • 2 replies
  • 367 views

I want to add a command to a key and I want to know how to do this. For example, if the user presses any key, they will move back one frame or forward a frame. Can anyone tell me the Actionscript for this? Thanks

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
February 1, 2013

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyUsed);  // or use KEY_UP

function keyUsed(event:KeyboardEvent)
{

         nextFrame();  // or prevFrame();
}

Nabren
Inspiring
February 1, 2013

You will need to add an event listener first:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

and then define the function

function keyDownHandler(e:KeyboardEvent):void

{

     switch(e.keyCode)

     {

          case Keyboard.W:

               // do something

               break;

     }

}

That would define a command to the W key. The Keyboard class has many constants for keys.