Question

As a follow up to this question: Developing a online exam application, how do I prevent cheaters?

Can I detect when Flex application looses its focus? that is if a user has clicked onto another application or opened a browser tab?

I read this: Detecting when a Flex application loses focus but was not very clear...

Was it helpful?

Solution

The key part of the code at that link is the

systemManager.stage.addEventListener(Event.DEACTIVATE,deactivate);

The Flash player send outs activate and deactivate events when the focus enters and leaves the player. All you need to do is create a listenr for them and react appropriately.

A more clear example of how to use to the activate and deactivate events can be seen at blog.flexaxamples.com.

Also, it looks like the activate and deactivate events have trouble in some browsers. Colin Moock has more info on that here.

OTHER TIPS

You can add a handler for activate in the main application tag. This detects whenever the flex application comes to focus. Eg:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" activate="activateHandler(event);" deactivate="deactivateHandler(event);">

This will work to detect when the Flex windows loses focus, but to detect when the window regains focus without having to actually click on the flex app requires an update in the HTML wrapper, correct? Something like:

<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = ${version_major};
// Minor version of Flash required
var requiredMinorVersion = ${version_minor};
// Minor version of Flash required
var requiredRevision = ${version_revision};
// -----------------------------------------------------------------------------
// -->


    function onAppFocusIn()
    {
        ${application}.onAppFocusIn();
        alert("onAppFocusIn");
    }

</script>
<body scroll="no" onFocus="onAppFocusIn()">

I am trying to implement this but the onAppFocusIn() function is not executing once I move back to the flex app window. When I view the source, the code is there. Does anyone know why??

Thanks, Annie

In Flex 4.6, this command works systemManager.stage.addEventListener(Event.DEACTIVATE, deactivate) but make sure the flash app wmode is set to window (default). When the wmode was transparent, the event didn't get caught. You set the wmode in the embedded html where you put your flash app. example:

<object classid="clsid:D27WEE-A16D-21cf-90F2-422253540410" width="100%" height="100%"
id="MyApp" name="MyApp" align="middle">
<param name="movie" value="MyApp.swf?v=1.00.008" />
<param name="wmode" value="transparent">      <----- take out this

...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top