Determining if a Flex application has focus using the activate and deactivate events

by Peter deHaan on February 29, 2008

in Application

The following example shows how you can determine if a Flex application has focus or not by listening for the activate and deactivate events on the <mx:Application /> container.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/29/determining-if-a-flex-application-has-focus-using-the-activate-and-deactivate-events/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        activate="application_activate(event);"
        deactivate="application_activate(event);">

    <mx:Script>
        <![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;

            private function application_activate(evt:Event):void {
                arrColl.addItemAt({type:evt.type, time:getTimer()}, 0);
            }

            private function time_labelFunc(item:Object, col:DataGridColumn):String {
                return numberFormatter.format(item[col.dataField]);
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl" />

    <mx:NumberFormatter id="numberFormatter"
            useThousandsSeparator="true" />

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="320"
            rowCount="8"
            verticalScrollPolicy="on">
        <mx:columns>
            <mx:DataGridColumn dataField="type" />
            <mx:DataGridColumn dataField="time"
                    headerText="time (ms)"
                    labelFunction="time_labelFunc" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

To test the previous example, try clicking within the Flex application and then click on the surrounding HTML, you should see the “activate” and “deactivate” events get dispatched and create new items in the DataGrid control.

{ 8 comments… read them below or add one }

1 Glendon February 29, 2008 at 9:19 am

This doesn’t work in safari 3 on a mac the way it’s supposed to. The browser doesn’t know when the OS regains focus. It works on Firefox on a mac though. So if you are going to have an important feature in your app. using this feature beware.

Reply

2 peterd February 29, 2008 at 2:29 pm

Glendon,

It seems to work on my Mac with the latest Safari.
What version of Flash Player are you using in Safari? http://blog.flexexamples.com/about-you/

Peter

Reply

3 Gustavo February 29, 2008 at 6:15 pm

Peter,
thanks for the previous reply.

I’ve tried the “about-you” and noticed the “hasScreenBroadcast”. (mine is set to false)
What is that? Can I broadcast my screen like using FMS without instaling a third-party program?

And I have an application that uses languages in an external .xml file.

How can I detect autmaticaly the language (like you did in the about-you) and load the respective .xml language file. (Now the users have to choose in each login using a ComboBox).

I think it would be something like: if language is pt, we load the portugues.xml and so on…

Thank you very much for your attention and this blog that I read every day,

Gustavo

Reply

4 peterd February 29, 2008 at 6:53 pm

Gustavo,

My Capabilities.hasScreenBroadcast property is false also. According to the documentation (link), the hasScreenBroadcast property:

Specifies whether the system does (true) or does not (false) support the development of screen broadcast applications to be run through Flash Media Server.

As for the languages, the code in the About You page just uses the Capabilities.language property (which returns “en” on my system).

trace(Capabilities.language); // en

I imagine it should be pretty easy to use a switch statement on the Capabilities.language property and load the appropriate language XML file (just make sure you set up a default block in case a user is using a language you don’t have an XML file for. For more information on the language property, see http://livedocs.adobe.com/flex/3/langref/flash/system/Capabilities.html#language.

Peter

Reply

5 Gustavo February 29, 2008 at 8:15 pm

Thank you very very much.

I’m new to computer programming as my focus is design.

Thanks to you now I know how I can probably set the languages automatically.

The screen broadcast is very important to me.

Do you know how this can be set to true? (because I don’t want to install a program just to simulate a camera that broadcasts my screen).

And if it’s not asking too much: what would be the approach for using this with FMS.
You can forget about this one if you don’t know :)

Thanks again and hope helping you one day,

Gustavo

Reply

6 Michael Eakes September 21, 2008 at 3:00 pm

Great test. Do you know of any way to force the flex app to “activate” (so that this event will be fired and keyboard focus enabled) without the user having to click on the application?

Reply

7 Ilya May 1, 2009 at 8:55 am

Thanks, this seems to work great for me except for one case: in Internet Explorer 7, while in application, when pressing an ‘ALT’ button, the application seems to loose focus (the browser goes to its menu: File, Edit, View, etc.), but the Event.DEACTIVATE doesn’t fire, even though the application doesn’t respond to mouse events anymore.
Do you know of any solution that can solve this?

Reply

8 Rogier October 3, 2009 at 2:07 pm

Hello,

Nice example, but it just doesn’t work anymore is Flex 3…

No response from activate and deactivate in the application header.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: