AS3快捷方式窗口并弹出

时间:2018-04-03 11:01:11

标签: actionscript-3 flex air flash-builder

我有一个带有几个窗口的AIR应用程序。 我已将快捷键ESC设置为关闭窗口。它运作良好。 在creationComplete上,我有这段代码:

this.addEventListener(KeyboardEvent.KEY_DOWN, exit_keyDownHandler);

 protected function exit_keyDownHandler(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.ESCAPE){
        // 
        this.removeEventListener(KeyboardEvent.KEY_UP, exit_keyDownHandler);
                stage.nativeWindow.close();

    }
}

但是在这个窗口,我可以打开一个弹出窗口,我在其中设置相同的快捷键(ESC)以删除弹出窗口。

在主窗口(第一个)上打开和关闭弹出窗口快捷方式后,无效!

我打开弹出窗口的代码如下:

wpTL = new wAddEditTL();
 PopUpManager.addPopUp(wpTL, this,true);
 PopUpManager.centerPopUp(wpTL); 

在弹出窗口中扩展TitleWindows,我添加了这个事件:

this.addEventListener(KeyboardEvent.KEY_DOWN, exit_keyDownHandler);

我希望编程专家可以帮助我,非常感谢你!

1 个答案:

答案 0 :(得分:1)

我有时间测试一下。

这是一个简单的解决方案,可以使用MX和Spak Window组件。

首先,我们创建一个简单的应用程序( Main.mxml ),只有两个按钮(打开MX或Spark窗口):

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                name="Key Events Test"
                horizontalAlign="center"
                verticalAlign="middle"
                creationComplete="creationCompleteHandler(event)"
                paddingLeft="0"
                paddingTop="0"
                paddingBottom="0"
                paddingRight="0"
                frameRate="60"
                width="320"
                height="200">

    <mx:Button id="sparkWindowButton"
               label="New Spark Window"
               click="sparkWindowButton_clickHandler(event)"/>

    <mx:Button id="mxWindowButton"
               label="New MX Window"
               click="mxWindowButton_clickHandler(event)"/>

    <mx:Script>
        <![CDATA[
        import mx.core.Window;
        import mx.events.FlexEvent;

        import spark.components.Window;

        private function creationCompleteHandler(event:FlexEvent):void {
            NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, systemManager_keyDownHandler);
        }

        private function systemManager_keyDownHandler(event:KeyboardEvent):void {
            if (event.keyCode == Keyboard.ESCAPE) {
                var currentWindow:NativeWindow = NativeApplication.nativeApplication.activeWindow;
                currentWindow.stage.nativeWindow.dispatchEvent(new Event(Event.CLOSING));
            }
        }

        private function sparkWindowButton_clickHandler(event:MouseEvent):void {
            var nativeWindow:spark.components.Window = new MySparkWindow();
            nativeWindow.open();
        }

        private function mxWindowButton_clickHandler(event:MouseEvent):void {
            var nativeWindow:mx.core.Window = new MyMXWindow();
            nativeWindow.open();
        }
        ]]>
    </mx:Script>
</mx:Application>

然后我们创建一个弹出组件( PopupPanel.mxml ):

<?xml version="1.0"?>
<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          title="POPUP"
          verticalAlign="middle"
          width="280"
          height="120">

    <mx:Label text="HEY"
              textAlign="center"
              width="100%"/>

</mx:Panel>

最后,我们创建了2个组件:

MX窗口( MyMXWindow.mxml ):

<?xml version="1.0"?>
<mx:Window xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:mx="library://ns.adobe.com/flex/mx"
           horizontalAlign="center"
           verticalAlign="middle"
           width="320"
           height="200"
           creationComplete="creationCompleteHandler(event)"
           showStatusBar="false">

    <mx:Button id="popupButton"
               label="Add a popup"
               click="popupButton_clickHandler(event)"/>

    <fx:Script>
        <![CDATA[
        import mx.events.FlexEvent;
        import mx.managers.PopUpManager;

        public var popup:PopupPanel;

        private function creationCompleteHandler(event:FlexEvent):void {
            this.title = "MX Window #" + NativeApplication.nativeApplication.openedWindows.length;
            this.stage.nativeWindow.addEventListener(Event.CLOSING, closingHandler, false,  0, true);
        }

        private function popupButton_clickHandler(event:MouseEvent):void {
            popup = new PopupPanel();
            PopUpManager.addPopUp(popup, this, true);
            PopUpManager.centerPopUp(popup);
        }

        private function closingHandler(event:Event):void {
            if (popup != null && popup.isPopUp) {
                event.preventDefault();
                trace("CLOSING POPUP");
                PopUpManager.removePopUp(popup);
            } else {
                trace("CLOSING WINDOW");
                stage.nativeWindow.close();
            }
        }
        ]]>
    </fx:Script>
</mx:Window>

最后,Spark窗口( MySparkWindow.mxml ):

<?xml version="1.0"?>
<s:Window xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          width="320"
          height="200"
          creationComplete="creationCompleteHandler(event)"
          showStatusBar="false">

    <s:layout>
        <s:VerticalLayout horizontalAlign="center"
                          verticalAlign="middle"/>
    </s:layout>

    <s:Button id="popupButton"
              label="Add a popup"
              click="popupButton_clickHandler(event)"/>

    <fx:Script>
        <![CDATA[
        import mx.events.FlexEvent;
        import mx.managers.PopUpManager;

        public var popup:PopupPanel;

        private function creationCompleteHandler(event:FlexEvent):void {
            this.title = "Spark Window #" + NativeApplication.nativeApplication.openedWindows.length;

            this.stage.nativeWindow.addEventListener(Event.CLOSING, closingHandler, false,  0, true);
        }

        private function popupButton_clickHandler(event:MouseEvent):void {
            popup = new PopupPanel();
            PopUpManager.addPopUp(popup, this, true);
            PopUpManager.centerPopUp(popup);
        }

        private function closingHandler(event:Event):void {
            if (popup != null && popup.isPopUp) {
                event.preventDefault();
                trace("CLOSING POPUP");
                PopUpManager.removePopUp(popup);
            } else {
                trace("CLOSING WINDOW");
                stage.nativeWindow.close();
            }
        }
        ]]>
    </fx:Script>

</s:Window>

现在,解释我们正在做的事情:

Main.mxml 中,我们正在监听整个应用程序的键盘事件(因此关注特定组件或窗口没有问题。)

当我们捕获ESCAPE键代码时,我们检索活动应用程序窗口的实例并向它发送一个Event.CLOSING事件以指示我们正在关闭窗口。

在窗口( MyMXWindow.mxml MySparkWindow.mxml )中,我们检索此事件并检查PopUp是否已打开。如果是这样,它将关闭,窗口继续工作,否则我们关闭窗口。

我不知道这是否是您想要做的,但我希望至少它会为您指出适合您项目的解决方案。

使用Flex和AS3进行快乐编码; - )