Saturday, September 4, 2010

Detecting focus on Flash using Actionscript 3

Sometimes it is needed to tell whether the user is focused on the Flash or not... in this tutorial, you will learn how to do that using AS3.

Here is what we will be making (click inside and outside of the flash):

We need to create 2 listeners - one for focusing in, and one for focusing out. These events are called Event.ACTIVATE and Event.DEACTIVATE.

stage.addEventListener(Event.ACTIVATE, inFocus);
stage.addEventListener(Event.DEACTIVATE, outFocus);

Now we need to show the user that he is focused out or in. Usually, this toggles the pause menu in Flash games, so that if a person focuses on something else, he will see that the game is paused and then proceed to click it to focus on it. In my case, I will just change a textfield's value. I put it on the stage manually and called it textF.

function inFocus(Event){

function outFocus(Event){
 textF.text="Not focused"

Event.ACTIVATE and Event.DEACTIVATE work in all browsers the same.

Hope this was helpful. Thanks for reading!

No comments:

Post a Comment