开发者

Is there a way to add a keyboardevent onto a flex top level application?

开发者 https://www.devze.com 2023-02-15 02:51 出处:网络
I\'m loading an image in to my flex application and I\'m trying to move the image via keyboard. I added an event listener to the application but the image will not move. How can I get it so that the f

I'm loading an image in to my flex application and I'm trying to move the image via keyboard. I added an event listener to the application but the image will not move. How can I get it so that the flex 4 top level application can use the keyboard listener. The only possible way I'm figuring out how to get the keyboard event开发者_运维知识库 listener to work is add to to a text field.

<?xml version="1.0" encoding="utf-8"?>

<fx:Declarations>

</fx:Declarations>

<s:creationComplete>
    <![CDATA[
        //this.addEventListener(MouseEvent.MOUSE_MOVE, movePlayer);
        this.addEventListener(KeyboardEvent.KEY_DOWN, movePlayerKeys);
    ]]>
</s:creationComplete>


<fx:Script>
    <![CDATA[
        import mx.core.FlexGlobals;
        private function movePlayer(e:MouseEvent): void {
            trace("Moving mouse",e.localX, e.localY);

        }

        private function movePlayerKeys(e:KeyboardEvent): void {
            trace("key pressed:",e.charCode);
            // add controls class here.
        }
    ]]>
</fx:Script>

<player:Player id="player" x="10" y="10"/>

Take into consideration I'm not even getting the trace on the keyboard event.


Use stage to add keyboard listeners, it works (if application has focus, of course).

0

精彩评论

暂无评论...
验证码 换一张
取 消