Detect Mouse Inactivity in Actionscript 3.0

Before we start you need to be familiar with MouseEvent and Timer classes.

var _timer:Timer = new Timer(5000);

function Start():void
{
   _timer.addEventListener(TimerEvent.TIMER, ShowMsg, false, 0, true);
   _timer.start();

   stage.addEventListener(MouseEvent.MOUSE_MOVE, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
}

function ShowMsg(e:TimerEvent):void
{
   trace("There is no activity");
}

function StopTimer(e:MouseEvent):void
{
   _timer.stop();
   _timer.start();
}

Start();

The first thing I’ve done is to setup a Timer object to count up to 5 seconds ( you can change that to what ever numer of seconds you want ).

The timer object also has an event listener so we can know when the 5 seconds are over.

I have setup some event listeners that will help detecting any actions of the mouse on the Stage ( MOUSE_DOWN, MOUSE_MOVE and MOUSE_UP )

If during those 5 seconds nothing happens ( mouse is not moved or performs a click ) ShowMsg() function will be fired up and give you a message of inactivity.

If the mouse is moved the StopTimer() function will be activated and reset timers count.

This is a useful and easy way to detect user activity.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s