Flash - 检测大键组合

时间:2012-10-12 15:42:22

标签: flash

我想识别按下的大键组合

Ctrl + Alt + Shift + D

例如,在动作中打开一些复活节彩蛋。

1 个答案:

答案 0 :(得分:0)

使用模型跟踪关键状态,然后在关键字上无效以测试您的组合评估是否为真:

package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 60)]
    public class X extends Sprite
    {

        private var controlKeyDown:Boolean = false;
        private var altKeyDown:Boolean = false;
        private var shiftKeyDown:Boolean = false;
        private var dKeyDown:Boolean = false;

        public function X()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
        }

        protected function keyDownHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.ALTERNATE:
                    altKeyDown = true;
                    break;
                case Keyboard.CONTROL:
                    controlKeyDown = true;
                    break;
                case Keyboard.SHIFT:
                    shiftKeyDown = true;
                    break;
                case Keyboard.D:
                    dKeyDown = true;
                    break;
            }

            invalidateKeyDown();
        }

        protected function keyUpHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.ALTERNATE:
                    altKeyDown = false;
                    break;
                case Keyboard.CONTROL:
                    controlKeyDown = false;
                    break;
                case Keyboard.SHIFT:
                    shiftKeyDown = false;
                    break;
                case Keyboard.D:
                    dKeyDown = false;
                    break;
            }
        }

        protected function invalidateKeyDown():Boolean
        {
            if (altKeyDown && controlKeyDown && shiftKeyDown && dKeyDown)
            {
                trace("easter egg.");
                return true;
            }

            return false;
        }

    }
}