Recently, a single-key shortcut key is used in a project, and a shortcut key management class is written.
Currently, this product can meet your needs and is easy to use.
Main functions:
- Each shortcut key can correspond to multiple methods
- Each shortcut key can be bound to a keyup or keydown event.
- You have "shortcut key + event + method" to confirm a binding. You can register or unregister the binding separately.
The following code is used ):
Package COM. douban. utils {import flash. display. stage; import flash. events. keyboardevent;/*** shortcut key manager * @ author seven Yu */public class shortcut cut {// register/deregister the shortcut key action Private Static const action_add: Int = 1; Private Static const action_del: int =-1; // Save the key-to-method private ING Private Static Var _ keymaps: Object ={}; // bind the stage Private Static Var _ stage: Stage of the keyboard listener; // indicates whether to start listening. (You can pause all listening and convert it to a non-static class. You can pause/listen as needed.) Private sta TIC VaR _ started: Boolean = false; // method counter corresponding to event type Private Static Var _ upfuncscount: Int = 0; Private Static Var _ downfuncscount: Int = 0; /*** initialize * @ paramstage to bind the keyboard event stage * @ paramautostart to automatically start listening (default: false) */public static function Init (stage: stage, autostart: boolean = false): void {_ stage = stage; autostart & START () ;}/ *** start listening for Keyboard Events */public static function start (): void {If (_ stage) {_ s Tarted = true ;}/ *** stop listening */public static function stop (): void {_ started = false ;} /*** process the shortcut key response * @ paramevt */Private Static function keyhandler (EVT: keyboardevent): void {trace ('event type: ', EVT. type, 'key code: ', EVT. keycode); If (! _ Started) {return;} var keyname: String = getkeyname (EVT. type, EVT. keycode); If (keyname in _ keymaps) {var funcs: array = _ keymaps [keyname]. concat (); For (var I: Int = 0, Len: Int = funcs. length; I <Len; I ++) {funcs [I] () ;}} /*** register the shortcut key * @ paramkeycode * @ paramfunc * @ paramtype */public static function register (keycode: int, FUNC: function, type: String = keyboardevent. key_up): void {trace ('short Cat. register ', keycode); var keyname: String = getkeyname (type, keycode); If (keyname in _ keymaps) {trace (_ keymaps [keyname]. indexof (func); If (_ keymaps [keyname]. indexof (func) =-1) {_ keymaps [keyname]. push (func); checkfuncs (type, action_add) ;}} else {_ keymaps [keyname] = [func]; checkfuncs (type, action_add );}} /*** cancel the shortcut key * @ paramkeycode * @ paramfunc * @ paramtype */public static function UN Register (keycode: int, FUNC: function, type: String = keyboardevent. key_up): void {trace ('cat cat. unregister ', keycode); var keyname: String = getkeyname (type, keycode); If (keyname in _ keymaps) {var funcindex: Int = _ keymaps [keyname]. indexof (func); If (funcindex> = 0) {_ keymaps [keyname]. splice (funcindex, 1); checkfuncs (type, action_del) ;}}/ *** check the number of function registrations to determine whether to bind or remove event listening * @ paramtype event type * @ parama Ction action (add or remove) */Private Static function checkfuncs (type: String, Action: INT): void {trace ('check funcs ', type, action); If (! _ Stage) {Throw new error ('Please call closing cut. init method first. ');} switch (type) {Case keyboardevent. key_up: {_ upfuncscount + = action; if (1 = _ upfuncscount) {_ stage. addeventlistener (keyboardevent. key_up, keyhandler);} If (0 = _ upfuncscount) {_ stage. removeeventlistener (keyboardevent. key_up, keyhandler);} break;} case keyboardevent. key_down: {_ downfuncscount + = action; if (1 = _ downfuncscount) {_ stage. addeventlistener (keyboardevent. key_down, keyhandler);} If (0 = _ downfuncscount) {_ stage. removeeventlistener (keyboardevent. key_down, keyhandler);} break;} default: {Throw new error ('keyboardevent. key_up & keyboardevent. key_down only. '); break ;}}} /*** obtain the ing name * @ paramtype event type * @ paramkeycode key value * @ return is a string such as type_keycode */Private Static function getkeyname (type: String, keycode: INT): String {return [type, keycode]. join ('_');}/*** start listening */static public function get started (): Boolean {return _ started ;}}}
Example: (pseudo code)
// Initialize the shortcut key manager shortcut cut. init (stage, true); // register space + keyup + testfunc1 (default keyup event) into cut. register (keys. spacebar, testfunc1); // register space + keydown + testfunc1shortcut. register (keys. spacebar, testfunc1, keyboardevent. key_down); // register S + keyup + testfunc2shortcut. register (keys. s, testfunc2, keyboardevent. key_up); // unregister spaces + keyup + testfunc1 (keyup is the default value and can be omitted) Escape cut. unregister (keys. spacebar, testfunc1, keyboardevent. key_up); // re-register e + keyup + testfunc2 (this combination is not reflected and has not been registered) using cut. unregister (keys. e, testfunc2); // test function 1 private function testfunc1 (): void {trace ('test function 1');} // test function 2 Private function testfunc2 (): void {trace ('test function 2 ');}
In addition, a keys class is also used in the Code to define the corresponding keycode constant of some key bits, which can be directly changed to the corresponding number, for example: space-> 32, S-> 83, e-> 69. There may be some immature things. You are welcome to share the discussion.