Flex: detecting user idle?
How can I tell when a user has been idle for say 5 minutes on my Flex app?
When I say "idle" I mean t开发者_如何学Gohe user has not interacted with the application at all.
Thanks!!
See also the idle
event in SystemManager
. This approach works for AIR or Flash Player.
application.systemManager.addEventListener(FlexEvent.IDLE, onIdle);
You can get the idle time (in an unsupported way) using
SystemManager.mx_internal::idleCounter
Being that this is an AIR app, I can just listen for the USER_IDLE event on the NativeApplication
//Set seconds for idle
this.nativeApplication.idleThreshold = 5;
//listen for user idle
this.nativeApplication.addEventListener(Event.USER_IDLE,lock);
Create a timer that you reset everytime you capture an user event at the application level.
If the timer has ended, then you know the user has been idle for that set amount of time.
// I am capturing only mouseMove and keyDown. That _should_ be enough to handle most user interactions.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" mouseMove="onUserEvent" keyDown="onUserEvent">
...
private function onUserEvent(event:Event):void
{
timer.reset();
}
You can get the time out by using following code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
initialize="init(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.core.mx_internal;
import mx.events.FlexEvent;
protected function init(event:FlexEvent):void
{
systemManager.addEventListener(FlexEvent.IDLE, handleApplicationIdle);
}
private function handleApplicationIdle(event:FlexEvent):void
{
if(event.currentTarget.mx_internal::idleCounter == 60){
Alert.show("Time out happened");
}
}
]]>
</fx:Script>
</s:Application>
@Michael Brewer-Davis
systemManager.addEventListener(FlexEvent.IDLE, onIdle)
works fine for mouse events.
What about Keyboard events. You have to have focus on some element before systemManager
listens to keyboard events.
Partial Solution:
On applicationComplete
event, I added the below line
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
Now keyboard events are getting listened.
Disadvantage: Only works after application is clicked atleast once. Then after works fine
Is there any way to make application listen to keyboard events without hassle of clicking once. Some suggested to add stage.focus = this
. Did not work either.(Still click was needed)
精彩评论