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).
精彩评论