ActionScript 3 Keyboard Events
February 6, 2009 by: smonteIn 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!
Related tutorials:
Comments
10 Responses to “ActionScript 3 Keyboard Events”Leave a Reply
You must be logged in to post a comment.
A very confusing tutorial with no tangible end result!
You have to set keyUpText and keyDownText as variables to retrieve the info.
http://www.mind4m.com/stories.php?pageid=15
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.
@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”);
}
if we draw another MovieClip (a red square for exemple) then we click on it, Are the event still dispatched to the listener ?
can u tell me key combination code for ctrl+c, ctrl+v
plzzzzzz
use &&
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
Plz help me, and yes i use ActionScript 3.0
Cool to know that but i get this error:
1120: Access of undefined property keyUpText.
1120: Access of undefined property keyDownText.