First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Handling keyboard events, play a key role in ActionScript, has been widely used especially in SWF games nowadays. It includes five types. I will introduce them with examples.
1. Handling keyboard events by checking with button.
2. Handling keyboard events with key object.
3. Handling keyboard events with keyboard listener.
4. Handling keyboard events with Function onKeyUp and onKeyDown of movie clip
5. Handling keyboard events with event keyUp and keyDown of movie clip.
You can get the amazing effect by mastering them.

Series: Sothink SWF Quicker 2.0

1. Handling keyboard events by checking with button.

You can either respond to mouse event or keystroke event with function "on" of button. I?l give you an example, write scripts for button and press X in keyboard, the output panel will deliver a message: X is pressed.

on (keyPress "x") {
trace("X is pressed");

Please notice that the letters should be lowercase while checking the letters in keyboard. Some specific scripts, which represent some special letters in SWF are convenient for you. The Action panel prepared completed events of mouse movement and key stroke.


For checking , you can use ActionScript as follows:

on (keyPress "") {
trace("Left is pressed");

You can input various "on" function or combine various events in a "on" function. Customizing your shortcut keys comes easily in this way. I will give you an example as follows:

on (keyPress "") {
on (keyPress "" ) {

The first sentence above means the movie clip myMC back one frame when you press Left keystroke. The second sentence means it steps up one frame when you press right keystroke.

Click keyboard1.rar (971 Bytes) to get source files.

2. Handling keyboard events with key object

For that it is not proper for durative pressed key though it functions well, it is always out of our choices for designing keyboard handled games, which we often use key object to make. Key object composed of method, constant and function are included in catalog "object/movie". You can check whether some keystroke is pressed or not. The following ActionScript can help you check whether Left keystroke is pressed.

if (Key.isDown(Key.LEFT)) {
trace("The left arrow is down");

Function "Key.isDown" backs a Boolean value. Function backs to true when the corresponding stroke parameter is pressed down, or it will back to false. Constant "Key. LEFT" represents Left stroke in keyboard. Function backs to true when Left keystroke is pressed down.

Some constants represent the corresponding keystrokes; I will show some basic constants:

The above ones are function keys. How to express letter keys of keyboard? Checking key code with function "Key.isDown()" can help you. Take an example as follows: the key code of letter X is 88.

if (Key.isDown(88)) {
trace("X is pressed");

The above codes means: use function Key.getCode to tell you whether you press X letter keystroke. Function "Key.isDown" backs to true when you press x letter keystroke. "X is pressed' will be outputted in output window.

Click keyboard2.rar (350 Bytes) to get source files.

3. Handling keyboard events with keyboard listener. (Recommended)

Suppose that you are checking keystroke action with event "onClipEvent(enterFrame)" of movie clip, and at the same time, the movie clip lasts for a great many frames with a low speed computer operation. Such things are likely to happen: the keystroke action is ignored when Function "onClipEvent(enterFrame)" does not function in time the moment you press the keystroke. If that, many effects you desired can not make.

Besides, there is another problem: handling keyboard events with key object can not distinct long lasting pressing a keystroke from fast and repetitious pressing a keystroke. In a lot of games, we need to execute an action by pressing a keystroke once, such as the game about fire or let fly (a missile) from a weapon. We even make long lasting pressing a keystroke as pressing keystroke only once. How to deal with this problem? Keyboard listener, which is for listening the keystroke action can help you.
First, you need to set a keystroke listener. You can use following action to tell program the event you need to listen:


Key.addListener takes object as its parameter, which is appointing to object, which can be taken to handle the event when listening event happens.
The above codes are handling the event by appointing to object. To let the object handle the event, it is meaningless to set listener if you don't set a corresponding event function. The functions of Keystroke listener are as follows: onKeyUp and onKeyDown.

_root.onKeyUp = function() {
//The ASCII code output in output panel the moment you press and release a keystroke.

Click keyboard3.rar (350 Bytes) to get source files.

4. Handling keyboard events with Function onKeyUp and onKeyDown of movie clip

It is similar to the method three. For that SWF can add listener to MovieClip, TextField and Button, etc., you can use function onKeyUp and onKeyDown directly.

One more thing should noticed is that onKeyDown can function only after you active and set input focus. First, set the properties of focusEnabled of movie clip as true. And then make sure this movie clip get focus. You can navigate the focus to movie clip by using function Selection.setFocus() or set the Tab key. Codes are as follows:

mc.tabEnabled = true;
//the movie get focus from Tab key. mc.focusEnabled = true;
//movie can get focus.
//set focus for movie.
mc.onKeyUp = function() {

The ASCII code output in output panel the moment you press and release a keystroke. The difference between method three and method four is that the former need not to add listener.

Please notice: it does not function when you drag mouse. You can get focus again by pressing Tab key because the movie clip lose focus now.

Click Keyboard4.rar (489 Bytes) to get source files.

5. Handling keyboard events with the event "keyUp" and "keyDown" of movie clip.

It is important to get the concept of this method very clearly before you use it.
Movie clip includes two events "keyUp" and "keyDown", which are related to keyboard. Such as follows:

onClipEvent (keyDown) {
//the ASCII code output in output panel the moment you press a keystroke;

"Key.getAscii" means to back to ASCII code, which is corresponding to keystroke. ASCII is an integer. Each letter of the keyboard has its own corresponding ASCII code, such as the ASCII of letter A is 65, "B" is 97, "+" is 43. Please notice: ASCII Code is only for Letters but not for function keystrokes. I will tell you how to input the corresponding letters of keystroke in output panel?

You can use "fromCharCode" of object "String" to change ASCII to letters. Continue use the above codes as an example. Can you find the difference?

onClipEvent (keyDown) {
//the corresponding letter output when you press a keystroke except function keystrokes.

Click keyboard5.rar (453 Bytes) to get source files.

Hopefully it is useful to many of you. Any time, Good luck!

» Level Intermediate

Added: 2006-05-14
Rating: 8 Votes: 7
» Author
With ActionScript, interactivity design and game making never become so easy. one year. I am happy to introduce this Flash Maker and share the learning experience with you friends in Flash Kit.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.