Flex timer使用 keydown事件注册到stage

Flex timer使用 keydown事件注册到stage:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 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="onApplicationComplete(event)">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            private var timer:Timer = new Timer(10,0);
            protected function onApplicationComplete(event:FlexEvent):void{
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown1);
                timer.addEventListener(TimerEvent.TIMER,ballMover);
            }
            protected function ballMover(event:TimerEvent):void{
                circle.x += 5;
                if(circle.x+100 >=stage.stageWidth){
                    trace('finished');
                    timer.stop();
                    Alert.show('Finished','PoweredCircle');
                }
            }
            protected function keyDown1(event:KeyboardEvent):void{
                // 必须点击主页面,获取焦点之后,才能捕获键盘事件;
                trace(event.toString());
                if(event.keyCode == 13){
                    timer.start();
                }
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <s:Group id="circle">
        <s:Ellipse width="100" height="100">
            <s:fill>
                <s:SolidColor color="#009900">
                </s:SolidColor>
            </s:fill>
            <s:stroke>
                <s:SolidColorStroke color="#000000" weight="2">
                </s:SolidColorStroke>
            </s:stroke>
        </s:Ellipse>
    </s:Group>
</s:Application>
原文地址:https://www.cnblogs.com/stono/p/4987418.html