Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » air » Detecting Idle User in AIR

Detecting Idle User in AIR

AIR has the ability to notify when the user is idle (no keyboard or mouse interaction) after a determined amount of time.

Adobe AIR has the ability to notify your application when the user is idle for a determine amount of time.

This idle time can be set be the developer and is similar to the idle monitors you will find in most popular chat clients

Note: This tutorial was written using Flex 3 Public Beta, but you can use Adobe Flash to create AIR content as well.

Adding this notification into a new or existing project is fairly simple. Start off by opening the main application ".mxml" file and add the following function to a new or existing script tag block.

private function init():void
{
  var nativeApp:NativeApplication = NativeApplication.nativeApplication;

  nativeApp.idleThreshold = 10;
  nativeApp.addEventListener(Event.USER_IDLE, userIdleHandler);
  nativeApp.addEventListener(Event.USER_PRESENT, userActiveHandler);			
}

The NativeApplication.nativeApplication is a static singleton (can only be one) instance of the current application. This singleton is assigned to a new variable making it easier to work with for the remainder of the code.

The idleThreshold is set to determine when a user is considered idle. The value is in seconds, which means this example is saying a user is marked idle after 10 seconds of no registered mouse or keyboard interaction

The two events that get attached to the native application assign the event handlers for the idle and active updates.

The next two functions are event handlers, called by the eventListeners assigned in the previous section of code

private function userActiveHandler(e:Event):void
{
  trace("User came back.");
}
		
private function userIdleHandler(e:Event):void
{
  trace("User is idle");
}

These two event handlers simply trace out the current status. In a more complete application you would probably change an image or update a text box depending on the application.

The last step in the code is to fire off the init() function, which is done by attaching it to the main application tag.

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
layout="absolute" initialize="init()">

Now you can test out the application by running the debug version. Leave the mouse and keyboard idle, and you will see the trace statements appearing in the output window. From this point you can add in more real world functionality as needed, but this should get you started

This was the first of many AIR articles to be featured on Scriptplayground. If you are an AIR developer and would like to have your tutorial on this site, feel free to contact me

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Detecting Idle User in AIR

 Jhecht  Tue Jan 8, 2008 12:01 am  
Quick question, how would you go about doing this in HTML and JS? I understand that you like flash and actionscript, that doesn't mean that we all do.
 mkeefe  Wed Jan 23, 2008 2:37 am  
I am aware of the Javascript side of things, and will be writing articles for that as well. I may even work out a system that each AIR tutorial has a JS and Flex part.
 Jacob  Mon Feb 7, 2011 8:37 am  
When you talk about a user being idle, are you talking about no user input to the application, or no user input to the computer?

If my application is minimized, is it still going to detect that the user is still using the computer?
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles