How to respond to keyboard events in Flash

Source: Internet
Author: User
Responding to keyboard methods as an important part of as, in today's increasingly widespread use, especially in the flash game production, if the lack of response to the keyboard method, it is not possible, and response to the keyboard method of the main four kinds, respectively:


1, using the button to detect


2, using Key object


3, using the keyboard to listen to the method


4, using the KeyUp and KeyDown events of the movie clips to implement the response keyboard


only mastered these methods, and then to adapt, you will get a lot of unexpected effects, I will combine the theory and the idea of their own brief introduction.


the first way to respond to the keyboard: Use the button to detect the response keyboard


can not only respond to mouse events in the button's on event handler, but can also respond to keyboard events. If you add the code shown below in the action panel of the button, you will be prompted in the Output window when tapping the X key on the keyboard: x is pressed


on the button, add:


on (keyPress "x") {


Trace ("X is pressed");


  }


but note that when you detect the letter keys on the keyboard, the letters should be lowercase. If you want to detect special keys in the keyboard, Flash has some special code to represent them, the following is a list of some commonly used function key representation code:


  


  


to detect keys on a keyboard, you can use the following actionscript:


on (keyPress "") {


Trace ("Left is pressed");


  }


Alternatively, you can add several on functions to a button, or you can combine multiple events in an on function, which allows you to define your own familiar shortcut keys for the button, as follows:


on (release, keyPress "") {


_root.mymc.prevframe ();


  }


on (release, keyPress "") {


_root.mymc.nextframe ();


  }


The first statement above implements clicking a button or pressing the LEFT ARROW key on the keyboard to control the movie clip MYMC back 1 frames, and the second statement of the above implements clicking the button or pressing the RIGHT ARROW key on the keyboard to control the movie clip MYMC forward 1 frames.


the second way to respond to the keyboard: using the Key object to implement the response to the keyboard (3lian footage)


button detection is very effective, but it is not conducive to the detection of continuously pressed keys, so it is not suitable for the production of some keyboard control game.


at this point, you need to use the key object. The key object is contained under the Objects/Movies directory in the Action panel, which consists of a series of methods, constants, and functions built into the flash. You can use the key object to detect whether a key is being pressed, and if you want to detect if the left ARROW key is pressed, you can use ActionScript:


if (Key.isdown (Key.left)) {


Trace ("The left Arrow was down");


  }

The
function Key.isdown returns a Boolean value that returns True if the key of the parameter in the number is pressed, or false. The constant key.left represents the left ARROW key on the keyboard. The function returns True when the left ARROW key is pressed.

The constants in the
key object represent the corresponding keys on the keyboard, and some basic constants are listed below:


the expression of some function keys:


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


above is the function key on the keyboard, then how to represent the letter keys on the keyboard?

The
Key object provides a function Key.getcode to implement this functionality, as follows:


if (Key.isdown (Key.getcode ("x")) {


Trace ("X is pressed");


  }


the above script means that you use the Key.getcode function to tell the system whether you pressed the X key, and if the X key is pressed, the function Key.isdown returns True and the x is pressed in the Output window.


Third Way to respond to the keyboard: Use the keyboard to listen to the method to implement the response keyboard (personal habits in this way)


assumes that the key action is detected in the Onclipevent (Enterframe) event handler of the movie clip, while the movie clip is on a longer axis, or the computer is running slower, It is possible that when a key is pressed on a keyboard before it can handle the onclipevent (enterframe) function, the key action will be ignored, so that many of the effects you want will not be realized.


Besides, there's another problem that needs to be solved in some games (such as shooting), we need to press the key once to perform an action (firing a bullet), even if a long time to hold down a key can only count as a key, and the key object can not be different is a long time to hold down the same key or a quick number of keystrokes.


so if you want to solve this problem, you need to use the method of listening to the keyboard. You can use the Listener (listener) to listen for keystrokes on your keyboard.


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.