Four methods for responding to Keyboard Events in Flash

Source: Internet
Author: User
The response keyboard method, as an important component of As, is now more and more widely used, especially in flash game production. If the response keyboard method is missing, it is impossible, there are four main methods for responding to the keyboard: 1. Use buttons for detection. 2. Use the key object. 3. Use the keyboard to listen. 4. Use the video clip's keyup and keydown events to respond to the keyboard

Only by mastering these methods and making some changes will you get a lot of unexpected results. Next I will give a brief introduction based on the theory and my own ideas.

The first method to respond to the keyboard: Use buttons for detection to implement the response keyboard

In the button's on event processing function, you can not only respond to mouse events, but also respond to Keyboard Events. Add the following code to the button action panel. When you press the X key on the keyboard, the output window will prompt: X is pressed.
Add:

On (keypress "X "){
Trace ("X is pressed ");
}

However, it should be noted that all the letters on the keyboard should be lowercase when detecting the keyboard's keyboard keys. If you want to detect the special keys on the keyboard, Flash has some special code to represent them. The following lists some commonly used function keys:

<Left> <right> <up> <down> <space <Home> <End> <Insert> <Pageup> <Pagedown> <enter> <Delete> <backspace> <Tab> <escape>

To detect the <left> key on the keyboard, you can use the following ActionScript:

On (keypress "<left> "){
Trace ("Left is pressed ");
}

In addition, you can add several on functions to a button, or combine multiple events in an on function. This allows you to define common shortcut keys for the button, as follows:

On (release, keypress "<left> "){
_ Root. mymc. prevframe ();
}
On (release, keypress "<right> "){
_ Root. mymc. nextframe ();
}

In the first statement above, click the button or press the left arrow key on the keyboard to control the video clip mymc to roll back one frame. In the second statement above, click the button or press the right arrow key on the keyboard, controls the video clip. mymc advances one frame.

Method 2: Use the key object to respond to the keyboard operation

Button detection is very effective, but it is not conducive to detecting keys that are continuously pressed, so it is not suitable for making some games controlled by the keyboard.

In this case, you need to use the key object. The key object is included in the "object"/"film" directory of the Action Panel. It consists of a series of built-in methods, constants, and functions of flash. You can use the key object to check whether a key is pressed. to check whether the left arrow key is pressed, you can use the following ActionScript:

If (key. isdown (key. Left )){
Trace ("the left arrow is down ");
}

The key. isdown function returns a Boolean value. If the key corresponding to the parameter in this number is pressed, true is returned. Otherwise, false is returned. Constant key. Left represents the left arrow key on the keyboard. When the left direction key is pressed, this function returns true.

The constants in the key object represent the corresponding keys on the keyboard. Some basic constants are listed below:

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 is the function key on the keyboard. How does it represent the function key on the keyboard?

The key object provides a function key. getcode to implement this function, as shown below:

If (key. isdown (key. getcode ("X "))){
Trace ("X is pressed ");
}

The script above means that the key is used. the getcode function tells the system whether you have pressed the X key. If the X key is pressed, the function key. isdown returns true, and X is pressed in the output window.

Third method to respond to the keyboard: Use the keyboard listening method to respond to the keyboard

Assuming that the key action is detected in the onclipevent (enterframe) event processing function of the video clip, and the timeline of the video clip is long or the computer operation speed is slow, this situation may occur: that is, when you press a key on the keyboard, you have not had time to process the onclipevent (enterframe) function, then the key action will be ignored. In this way, many of the effects you want will not be achieved.

In addition, there is another problem to be solved: In some games (such as shooting), we need to press a key to execute an action (trigger a bullet ), even if you hold down a key for a long time, it can only be counted as one key. The key object cannot be different from holding down the same key for a long time or pressing the key multiple times quickly.

To solve this problem, you need to use the keyboard listening method. You can use the "listener" to listen for key actions on the keyboard.

Before using the listener, you must first create it. You can use the following command to tell the computer that you need to listen for an event:

Key. addlistener (_ root );

The key. addlistener command uses the main timeline or a video clip as its parameter. When a listening event occurs, you can use the object specified by this parameter to respond to the event.

The above Code specifies the main timeline to respond to this event. To make the main timeline respond to this event, you also need to set an event handler function. Otherwise, setting the listener makes no sense.

There are two event handler functions for keyboard listening: onkeyup and onkeydown, as shown below:

Key. addlistener (_ root );
_ Root. onkeyup = function (){
Trace (key. getascii ());
};
// The code means that when you press a key and release it, the output window will output the ASCII code of the key you press.

Of course, you can also use video editing as the keyboard listening object. You only need to use the video editing path instead of _ root as the parameter of the key. addlistener command. For example, the following code:

Key. addlistener (_ root. MC );
_ Root. MC. onkeyup = function (){
Trace (key. getascii ());
};

The code means that when you press the next key and release it, the output window will output the ASCII code of the key you press. The meaning is similar, but the keyboard listening object is different. One is the film MC, one is the main timeline.Method 4: Use the video clip's keyup and keydown events to respond to the keyboard

The last method is easy to ignore, but it also has some application value. The most important thing is to clarify the concept.
Video Clips contain two keyboard-related events, keyup and keydown. You can use these events to respond to key events.
For example, the following code:

Onclipevent (keydown ){
Trace (key. getascii ());
}
// When you press a key on the keyboard, the output window outputs the ASCII value of the key.

Function key. getascii indicates the ASCII code corresponding to the key. The ASCII code is an integer, and each character on the keyboard corresponds to an ascii code. For example, the ASCII code corresponding to letter A is 65, b's ASCII code is 66, A's ASCII code is 97, B's ASCII code is 98, and +'s ASCII code is 43. Note that only character keys have ASCII codes. The function keys on the keyboard do not have ASCII codes.

What if I want to output the character corresponding to the button in the output window?

At this time, you can use the fromcharcode function of the string object to convert the ASCII code into characters, as shown in the code in the above example:

Onclipevent (keydown ){
Trace (string. fromcharcode (key. getascii ()));
};
// This means that when you press a key on the keyboard, the character corresponding to the key is output, except for the function key.

For a detailed explanation of the string object, you can view the "object"/"core" directory of the Action panel.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.