Drew,

What you want to do is of course very common, but surprisingly, the way to pull 
it off isn't 
very obvious. In fact, even the documentation is a little inaccurate on this 
topic (at the time 
of this writing). Here's a solution.

In order to listen globally for keyboard events you need to register a KEY_UP 
or KEY_DOWN 
listener with the Stage, not the Application. In Flex 2 the Stage is not 
globally accessible 
(as it was in previous versions of Flex) but instead is accessible via the 
"stage" property of 
any DisplayObject, including Application.

One tricky hurdle to get over is when exactly you should register the listener. 
It turns out, 
if you try to register it too early - for example upon the Application's 
"initialize" or 
"creationComplete" events, the "stage" property doesn't contain a reference to 
the Stage, 
yet. Instead, you should trigger your event registration code from the 
Application's 
"applicationComplete" event. Here's a little demo...

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 
applicationComplete="registerKeyListener()">
        <mx:Script>
                <![CDATA[
                        public function registerKeyListener() :void
                        {
                                stage.addEventListener(KeyboardEvent.KEY_DOWN, 
handleKeyDown);
                        }
                        
                        public function handleKeyDown(event:Event) :void
                        {
                                trace("Heard KEY_DOWN");
                        }
                ]]>
        </mx:Script>
</mx:Application>





--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/flexcoders/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:[EMAIL PROTECTED] 
    mailto:[EMAIL PROTECTED]

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to