我有一个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
并追踪一些信息,但没有。
答案 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>