Flex losing KeyboardEvents
One of those "in case anyone else has this problem" postings...
Flex 4 has an exasperating bug whereby the keyboard focus can be lost, causing your KeyboardEvent listeners not to fire when a key is pressed. Generally this seems to happen when a UI element with focus, such as a dialogue box, is closed: the focus stays on the (now non-existent) dialogue box button, rather than returning to the stage. Consequently, any KeyboardEvent listeners attached to the stage won't trigger.
One commonly suggested solution is to add "stage.focus=stage" to the function you call when closing the dialogue box. But that's a bit nasty if you have 20 dialogues in your app - you really shouldn't have to add custom code to each one.
So, instead, I chose to add this to the Event.ENTER_FRAME function that I have running anyway (but I suspect there are probably other hooks you can add it to):
if (stage.focus && !stage.contains(stage.focus))
{ stage.focus=stage; }
This simply checks that the focused element is actually on the stage, and if not, sets the focus back to the stage.
