views:

1037

answers:

2

I have an small application in flex in which I have defined 2 canvases. On one of them I added controls and the other one is used to draw something and no controls are added:

<mx:Canvas x="0" y="80" width="100%" height="520%" id="Canvas1"/>
<mx:Canvas x="0" y="0" width="100%" height="80" id="Canvas2"/>

I add an keydown event handled to the application but it is triggered only after I click with the mouse on the first Canvas(the one where controls have been added). Otherwise the event is not triggered when keys are presses.

I've tried several things: to set focus on the second canvas, to add the keydown handler to the application(this),stage, canvas... Unfortunately I didn't find a solution to trigger keydown no matter where the focus is.

A: 

The first problem you describe (having to click the mouse before any KeyboardEvents are dispatched) is likely due to the browser not giving focus to the Flex application itself. You can use Javascript in the HTML wrapper to programmatically give focus to the Flex app on the "body" element's "onLoad" event. I know for certain this works in Firefox and I believe it works okay in IE as well. I'll try to dig up the relevant Javascript code.

As far as the second problem, have tried adding an event listener to Application.application for catching all KeyboardEvents?

cliff.meyers
A: 

Could you plase post code snipped how you setup the listener for keyboard and how you handle it?

vranac