ActionScript 3 Keyboard Events

February 6, 2009 by: smonte

In order to create a fully interactive flash movie, you need to know something about keyboard events. This tutorial is all about catching ActionScript 3.0 keyboard events and responding to them. So let’s get started!

Click on the stage and start pressing some buttons. See how the values change. Now let’s look the code behind this movie.

Moving to ActionScript 3.0

Here is the code behind the ActionScript keyboard event movie.

 
stage.addEventListener (KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyUpHandler);
 
function keyDownHandler (e:KeyboardEvent):void {
 
        keyDownText.text = 
        "Key code: " + e.keyCode + "\n" +
        "Ctrl status: " + e.ctrlKey + "\n" +
        "Key location: " + e.keyLocation + "\n" +
        "Shift key: " + e.shiftKey + "\n";
}
 
function keyUpHandler (e:KeyboardEvent):void {
 
        keyUpText.text = "Key code: " + e.keyCode;
}

As you can see, the code is really simple. The “keyDownText” and “keyUpText” are just dynamic text fields, where we put the keyboard event text. Here are the explanations for the keyboard event properties.

keyCode: Every keyboard button has a numerical key code value. You don’t need to remember them of course. For example, use this movie to find the key’s corresponding value.

ctrlKey: Tells whether the control key is pressed or not. There is also a same kind of property for the alt button called “altKey”.

keyLocation: This property indicates the location of the key on the keyboard. For example press the right shift button and see the location value for it. Then press the left shift button. As you can see, the location value changes. This is how you can differentiate same keys in the keyboard. This comes especially useful if you want to differentiate the keys between a numeric keypad and a standard keyboard.

shiftKey: Tells whether the shift key is pressed or not.

That’s it for this tutorial. Now you should be able to include keyboard interaction in your movies!

  • Digg
  • Sphinn
  • del.icio.us
  • Mixx

Related tutorials:

  1. Physics with ActionScript 3
  2. ActionScript 3.0 Events
  3. Interactive Rocket
  4. ActionScript 3 Color Picker
  5. ActionScript 3 Timer Class

Comments

10 Responses to “ActionScript 3 Keyboard Events”
  1. JenkinsW says:

    A very confusing tutorial with no tangible end result!

    Log in to Reply
  2. KC Hunter says:

    You have to set keyUpText and keyDownText as variables to retrieve the info.

    http://www.mind4m.com/stories.php?pageid=15

    Log in to Reply
  3. Josh says:

    1120: Access of undefined property keyUpText.
    1120: Access of undefined property keyDownText.

    This is because you haven’t created a keyUpText or keyDownText object. Use your text tool and create two text fields that read “Key Up” and “Key Down”. Under the properties tab down at the bottom, change the instance names of those text object to keyUpText and keyDownText, respectively.

    Log in to Reply
  4. @sachin
    Check for “e.ctrlKey” and your needed “e.keyCode”.

    Do this in the KEY_UP handler.
    Doing so in the KEY_DOWN handler is somehow not working for me.

    function keyUpHandler (e:KeyboardEvent):void {
    keyUpText.text = “Key code: ” + e.keyCode;
    if(e.keyCode == 83 && e.ctrlKey) trace(“CTRL+S”);
    else if(e.keyCode == 90 && e.ctrlKey) trace(“CTRL+Z”);
    }

    Log in to Reply
  5. Vincs says:

    if we draw another MovieClip (a red square for exemple) then we click on it, Are the event still dispatched to the listener ?

    Log in to Reply
  6. sachin says:

    can u tell me key combination code for ctrl+c, ctrl+v
    plzzzzzz

    Log in to Reply
  7. smonte says:

    Hi,

    To create a movie like I have, you need to create two dynamic text fields on the stage. Give them instance names of “keyDownText” and “keyUpText”.

    Hope this clears things up! The tutorial’s point is to look at the KeyboardEvent, not about how to create the Flash movie. Sorry for the inconvinience.

    -smonte

    Log in to Reply
  8. Myrna L. says:

    Plz help me, and yes i use ActionScript 3.0

    Log in to Reply
  9. Myrna L. says:

    Cool to know that but i get this error:

    1120: Access of undefined property keyUpText.
    1120: Access of undefined property keyDownText.

    Log in to Reply

Leave a Reply

You must be logged in to post a comment.