Friday, April 6, 2007

How To.Global Keyboard Listener

If you've ever tried listening to the Application for a KeyboardEvent you'll know that the listener doesn't always fire. To fix this, add the event listener to the stage. You can get to the stage by accessing the stage property of any DisplayObject. Example:

Application.application.stage.addEventListener(KeyboardEvent.KEY_DOWN,globalKeyDownListener);

5 comments:

Anonymous said...

Okay I'm at a loss, the I have tried this method but the moment the user clicks anything inside the application, the application looses focus and the event listener quits, how do you get one that is truly "Global"?

Dan said...

I'm not sure why it's not working for you, but from personal experience and anecdotal evidence I believe it does work in general. You should post your question with code to flexcoders to troubleshoot the specific issue you've found. I'll update the post if we find anything.

Anonymous said...

I've been on this for ages. I can't find a way to keep the focus. Lets say I used this and then I clicked a ComboBox, pressed down and... wala. It scrolls instead of going to the keyboard...

jj said...

i tried it too, it doesn't work

whit nelson said...

Yeah, this needs to be revisited. Your code now gives TypeError: Error #1009: Cannot access a property or method of a null object reference.

"Flex", "ActionScript" and possibly "MXML" are probably trademarks of Adobe Systems Incorporated.
"Adobe" is a trademark of Adobe Systems Incorporated.
This site is in no way endorsed or sponsored by Adobe Systems Incorporated.
Content Copyright © 2007 Daniel Freiman.
Site Design Copyright by its copyright holder.
The Flex Non-Docs reserves the right to remove comments for any reason.
All ActionScript and MXML code (and ONLY ActionScript and MXML code) on this website is available under the MIT License.