Handling Keyboard Events
Handling keyboard events, play a key role in ActionScript, has been widely used especially in SWF games nowadays. It includes five types:
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: 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’ll give you an example, write scripts for button and press X in keyboard, the output panel will deliver a message: X is pressed.
Code: |
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 <Left>, you can use ActionScript as follows:
Code: |
on (keyPress "<Left>") { 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’ll give you an example as follows:
Code: |
on (keyPress "<Left>") { _root.myMC.prevFrame(); } on (keyPress "<Right>") { _root.myMC.nextFrame(); } |
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.
Code: |
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:
Key.BACKSPACE Key.ENTER Key.PGDN
Key.CAPSLOCK Key.ESCAPE Key.RIGHT
Key.CONTROL Key.HOME Key.SHIFT
Key.DELETEKEY Key.INSERT Key.SPACE
Key.DOWN Key.LEFT Key.TAB
Key.END Key.PGUP Key.UP
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.
Code: |
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:
Code: |
Key.addListener(object); |
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.
Code: |
Key.addListener(object); _root.onKeyUp = function() { trace(Key.getAscii()); }; //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:
Code: |
mc.tabEnabled = true; //the movie get focus from Tab key. mc.focusEnabled = true; //movie can get focus. Selection.setFocus(mc); //set focus for movie. mc.onKeyUp = function() { trace(Key.getAscii()); }; stop(); |
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:
Code: |
onClipEvent (keyDown) { trace(Key.getAscii()); } //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 what’s the difference?
Code: |
onClipEvent (keyDown) { trace(String.fromCharCode(Key.getAscii())); }; //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!