Spark WindowedApplication,监控keyUp事件

时间:2013-03-19 12:50:51

标签: windows flex air flex-spark

我有一个WindowedApplication,我需要监视一个keyUp,以便我可以在按下的转义键上关闭应用程序。但是,我的代码无效:

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       keyUp="windowedapplication1_keyUpHandler(event)">

<fx:Script>
        <![CDATA[   

            protected function windowedapplication1_keyUpHandler(event:KeyboardEvent):void
            {
                //Monitor key press {ESC}
                if(event.keyCode == 27){
                    exit();
                }
                else{
                    trace("key = " + event.keyCode);
                }
            }   

        ]]>
</fx:Script>

似乎此事件根本没有触发,我甚至试图覆盖keyUpHandler并追踪一些信息,但没有。

1 个答案:

答案 0 :(得分:1)

我在Adobe论坛上找到了this similar question,它帮助解决了这个问题。

我通过侦听applicationComplete然后在处理程序中将keyUp侦听器附加到stage来解决。

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       applicationComplete="init()">

<fx:Script>
        <![CDATA[   

            protected function init():void
            {
                stage.addEventListener(KeyboardEvent.KEY_UP, windowedapplication1_keyUpHandler);
            }

            protected function windowedapplication1_keyUpHandler(event:KeyboardEvent):void
            {
                //Monitor key press {ESC}
                if(event.keyCode == 27){
                    exit();
                }
                else{
                    trace("key = " + event.keyCode);
                }
            }       

        ]]>
</fx:Script>