Saturday, February 13, 2010

Flex Activate / Deactivate Events on OS X

In JamLegend, it would be nice to auto-pause the game when the user switches focus to another application. You'd think it would be very simple to detect if Flash player currently has focus, but the recommended methods are unreliable cross-platform.

Findings (so far)
  • Chrome, Firefox, and Safari receive activate and deactivate events if the browser loads the SWF directly, but NOT if the SWF is loaded inside of an HTML page.
  • The method of loading the SWF is inconsequential; e.g. JavaScript vs. HTML tags.
  • When the SWF is loaded directly, activate is not dispatched when re-focusing on the browser by clicking the flex app; a second click is required.
This is really a pain. One suggested hack is to look for the MOUSE_LEAVE event (when the user moves the mouse off of the flex app). That works well when UI elements are draggable and you need to handle the case of the mouse leaving the flex app while dragging, but it isn't a valid indicator of whether the flex app still has focus. For example, in JamLegend, the user can move the mouse anywhere and they can still play as long as the flex app has focus (thus receiving key events).

There might be a big hack somewhere, utilizing activate and deactivate events for Windows, plus a combo of JavaScript focus events and mouse position for OS X, but I haven't figured it out yet...

System Details
  • OS X 10.6.2
  • Flash Player 10,0,42,34
  • Chrome 5.0.307.7 beta
  • Safari 4.0.4
  • Firefox 3.5.5.