Flash/flex Study Notes (17): Key capture

Source: Internet
Author: User
Tags addchild

Let's take a look at a simple single key Capture:

Package {import flash. display. sprite; import FL. controls. label; import flash. events. keyboardevent; import flash. UI. keyboard; public class keydown extends sprite {private var LBL: Label; private var ball: SPRITE; Public Function keydown (): void {Init ();} private function Init (): void {stage. focus = This; // n indicates that you must set the focus first. But in actual tests, it seems that you can handle keyboard events without adding this line? LBL = new label (); LBL. TEXT = "press the key. Here your key value is displayed. You can move the ball by pressing the arrow key"; LBL. autosize = "center"; addchild (LBL); LBL. width = stage. stagewidth; LBL. height = 20; LBL. move (0, 10); ball = new sprite (); addchild (ball); // draw a ball. graphics. beginfill (0xff0000); ball. graphics. drawcircle (0, 0, 30); ball. graphics. endfill (); // locate the stage center ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; stage. addeventlistener (keyboardevent. key_down, keydownhandler );} Public function keydownhandler (E: keyboardevent): void {LBL. TEXT = "your key value is:" + E. keycode. tostring () + "; press" + String. fromcharcode (E. keycode); Switch (E. keycode) {Case keyboard. up: ball. y-= 10; break; Case keyboard. down: ball. Y + = 10; break; Case keyboard. left: ball. x-= 10; break; Case keyboard. right: ball. X + = 10; default: break;} If (E. ctrlkey) {LBL. TEXT = "you press Ctrl! ";} If (E. shiftkey) {LBL. Text =" you have pressed the Shift key! ";}// Note: In fact, in many browsers, including flash players, ALT is used as the menu activation key by default, so ALT is blocked by them, as a result, flash cannot capture if (E. altkey) {LBL. TEXT = "you have pressed the Alt key! ";}}}}

Let's take a look at the key combination capture similar to A + B + C:

 

Analyze the process first. For example, when you press Ctrl + A, you actually press Ctrl and trigger the keydown event at the same time, and then press a while holding down CTRL, trigger the keydown event again and then release (trigger the keyup event). This is a sequential process.

Train of Thought: When you press the key and haven't released any key, you can consider using a piece of data to store all the key values pressed this time, and then wait for the user to release it. Once released, it can be considered that the key combination is completed, and then the data is cleared and ready for use next time. In this way, the key combination is saved in the array.

Follow these steps to improve the above Code:

Package {import flash. display. sprite; import FL. controls. label; import flash. events. keyboardevent; import flash. UI. keyboard; import flash. UI. *; public class keydown extends sprite {private var LBL: Label; private var ball: SPRITE; private var keyvaluearr: array; // when capturing the key combination, used to store all the pressed key values of this time (before the keyup event is triggered) Private var keynamearr: array; // The character public function keydown () corresponding to the key value (): void {Init ();} private function Init (): void {sta GE. Focus = This; // n indicates that you need to set the focus first. But in actual tests, it seems that you can handle keyboard events without adding this line? LBL = new label (); LBL. TEXT = "press the key (CTRL and shift keys are supported). Here, your key value is displayed. You can move the ball by pressing the arrow key"; LBL. autosize = "center"; addchild (LBL); LBL. width = stage. stagewidth; LBL. height = 20; LBL. move (0, 10); ball = new sprite (); addchild (ball); // draw a ball. graphics. beginfill (0xff0000); ball. graphics. drawcircle (0, 0, 30); ball. graphics. endfill (); // locate the stage center ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; stage. addeventlistener (keyboardevent. key_do Wn, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler); keyvaluearr = new array (); keynamearr = new array ();} public function keydownhandler (E: keyboardevent): void {// note: since ALT is always intercepted by the browser or the player menu bar, if (! (E. keycode = keyboard. shift | E. keycode = keyboard. control) {keyvaluearr. push (E. keycode); keynamearr. push (string. fromcharcode (E. keycode);} LBL. TEXT = "your key value is:" + E. keycode. tostring () + "; press" + String. fromcharcode (E. keycode); Switch (E. keycode) {Case keyboard. up: ball. y-= 10; break; Case keyboard. down: ball. Y + = 10; break; Case keyboard. left: ball. x-= 10; break; Case keyboard. right: ball. X + = 10; default: break;} If (E. ctrlkey) {If (keyvaluearr. length> 0) {LBL. TEXT = "you press Ctrl +" + keynamearr. join (',') ;}} if (E. shiftkey) {If (keyvaluearr. length> 0) {LBL. TEXT = "Shift +" + keynamearr. join (',') ;}} public function keyuphandler (E: keyboardevent): void {keyvaluearr. length = 0; keynamearr. length = 0 ;}}}

Finally, let's take a look at the so-called "" movement: many games can use the arrow keys to control the movement direction of characters. In the above example, only four directions can be moved horizontally and vertically, if you want to move in the 8 direction, you need to use the key combination and make some modifications to the above Code:

Package {import flash. display. sprite; import FL. controls. label; import flash. events. keyboardevent; import flash. UI. keyboard; import flash. UI. *; public class keydown extends sprite {private var LBL: Label; private var ball: SPRITE; private var keyvaluearr: array; // when capturing the key combination, used to store all the pressed key values of this time (before the keyup event is triggered) Private var keynamearr: array; // The character public function keydown () corresponding to the key value (): void {Init ();} private function Init (): void {sta GE. Focus = This; // n indicates that you need to set the focus first. But in actual tests, it seems that you can handle keyboard events without adding this line? LBL = new label (); LBL. TEXT = "press the key (CTRL and shift keys are supported). The key value is displayed here. You can move the ball by pressing the arrow key (8 direction supported)"; LBL. autosize = "center"; addchild (LBL); LBL. width = stage. stagewidth; LBL. height = 20; LBL. move (0, 10); ball = new sprite (); addchild (ball); // draw a ball. graphics. beginfill (0xff0000); ball. graphics. drawcircle (0, 0, 30); ball. graphics. endfill (); // locate the stage center ball. X = stage. stagewidth/2; ball. y = stage. stageheight/2; stage. addeventlistener (keyboardevent. Key_down, keydownhandler); stage. addeventlistener (keyboardevent. key_up, keyuphandler); keyvaluearr = new array (); keynamearr = new array ();} public function keydownhandler (E: keyboardevent): void {// note: since ALT is always intercepted by the browser or the player menu bar, if (! (E. keycode = keyboard. shift | E. keycode = keyboard. control) {If (keyvaluearr. indexof (E. keycode) =-1) {keyvaluearr. push (E. keycode); keynamearr. push (string. fromcharcode (E. keycode) ;}} LBL. TEXT = "your key value is:" + keyvaluearr. join (',') + "; press" + keynamearr. join (','); // If (keyvaluearr. length = 1) {Switch (E. keycode) {Case keyboard. up: ball. y-= 10; break; Case keyboard. down: ball. Y + = 10; break; Case keyboard. left: Ba Ll. x-= 10; break; Case keyboard. right: ball. X + = 10; default: break;} else if (keyvaluearr. length> 1) {// trace (keyvaluearr. join (','); If (keyvaluearr. indexof (Keyboard. up )! =-1 & keyvaluearr. indexof (keyboard. Left )! =-1) {// ball. X-= 10; ball. Y-= 10;} else if (keyvaluearr. indexof (keyboard. Up )! =-1 & keyvaluearr. indexof (keyboard. Right )! =-1) {// ball. x + = 10; ball. Y-= 10;} else if (keyvaluearr. indexof (keyboard. Down )! =-1 & keyvaluearr. indexof (keyboard. Right )! =-1) {// ball. x + = 10; ball. Y + = 10;} else if (keyvaluearr. indexof (keyboard. Down )! =-1 & keyvaluearr. indexof (keyboard. Left )! =-1) {// bottom left ball. x-= 10; ball. Y + = 10 ;}} if (E. ctrlkey) {If (keyvaluearr. length> 0) {LBL. TEXT = "you press Ctrl +" + keynamearr. join (',') ;}} if (E. shiftkey) {If (keyvaluearr. length> 0) {LBL. TEXT = "Shift +" + keynamearr. join (',') ;}} public function keyuphandler (E: keyboardevent): void {keyvaluearr. length = 0; keynamearr. length = 0 ;}}}

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.