Tuesday, December 23, 2008

Determining Inactivity in Flex (FlexEvent.IDLE)

A FlexEvent.IDLE event is dispatched every 100 milliseconds when there is no keyboard or mouse activity for 1 second.

In order to use the following event we need to import the following classes.
1. import mx.managers.SystemManager;
2. import mx.events.FlexEvent;
3. import mx.core.mx_internal;

You’ll need to tell your program that you want to use mx_internal as a namespace to access those properties within the SystemManager class. SytemManager has an “idleCounter” property which is super useful to access- but it is scoped to mx_internal and is not normally accessible. Trying to access it without these steps will throw an error:

1. use namespace mx_internal;

SystemManager is automatically instantiated as a part of every Flex app, so we do not need to do this manually. We will, however, need to add an event listener for FlexEvent.IDLE:

1. this.systemManager.addEventListener(FlexEvent.IDLE, userIdle);

Construct the callback method. One minutes is equal to 60000 milliseconds… divided by 100 ticks and the number we need to check against is 600. Of course, you’ll probably want something a little shorter in duration for testing:

private function userIdle(e:FlexEvent):void {
if(e.currentTarget.mx_internal::idleCounter == 600){
//do something!

Note that we prefix the idle Counter property with the mx_internal namespace.

That’s it! Now we have a sweet little activity monitor in our app. When activity is detected, idle Counter will automatically reset as well.

No comments:

About Me