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.
<?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.





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.
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
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
Gustavo,
My
Capabilities.hasScreenBroadcastproperty is false also. According to the documentation (link), thehasScreenBroadcastproperty:As for the languages, the code in the About You page just uses the
Capabilities.languageproperty (which returns “en” on my system).I imagine it should be pretty easy to use a switch statement on the
Capabilities.languageproperty 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
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
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?