How to detect multiple key down event in as3?

后端 未结 1 1150
刺人心
刺人心 2020-12-11 13:24

I just got started learning AS3.

Let\'s say I have two textfields on my sprite.

I like to move textfield 1 when I press left or right arrow keys, but I also

相关标签:
1条回答
  • 2020-12-11 13:43
    package  {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.KeyboardEvent;
        import flash.ui.Keyboard;
        /**
         * ...
         * @author www0z0k
         */
        public class KeyExample extends Sprite {
            private var _theyArePressed:Object = { };
            public function KeyExample() {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
            }
    
            private function init(e:Event):void {
                removeEventListener(Event.ADDED_TO_STAGE, init);
                stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
                stage.addEventListener(KeyboardEvent.KEY_UP, onUp);         
            }
    
            private function onUp(e:KeyboardEvent):void {
                _theyArePressed[e.keyCode] = false;
            }
    
            private function onDown(e:KeyboardEvent):void {
                _theyArePressed[e.keyCode] = true;
                if (_theyArePressed[Keyboard.SPACE] && _theyArePressed[Keyboard.UP]) {
                    //do anything
                }
            }       
        }
    }
    

    but keep in mind that that AFAIK keyboards can handle limited quantity of keys pressed at the same time, depending on the hardware

    0 讨论(0)
提交回复
热议问题