Detecting user inactivity

Fun Fun Fun… yet another utility class for your guys’ using pleasure.

Saved as, IdleUserWatcher.as


class  IdleUserWatcher
{
    // is the user active?
    private var __isActive : Boolean = false;
    // the id for the interval
    private var intervalID : Number;
    // how long to wait before calling is idle... default is 10 seconds
    private var idleTime : Number = 1000;
    // a list of all objects listening
    private var listeners : Array;
    // readonly property for isActive
    public function get isActive () : Boolean
    {
        return __isActive;
    }
    
    public function IdleUserWatcher ( idleTime:Number )
    {
        if (idleTime != undefined)
        {
            this.idleTime = idleTime;
        }
        
        Mouse.addListener (this);
        Key.addListener (this);
        listeners = new Array();
        
    }
    /* Adds a listener to the listeners list */
    public function addListener(listener:Object) : Boolean
    {
        for (var i:Number=0; i<listeners.length; i++)
        {
            if (listeners* == listener) return false;
        }
        listeners.push(listener);
        return true;
    }
    /* Removes a listener from the listener list */
    public function removeListener(listener:Object) : Boolean
    {
        for (var i:Number=0; i<listeners.length; i++)
        {
            if (listeners* == listener)
            {
                listeners.splice(i, 1);
                return true;
            }
        }
        return false;
    }
    
    /* Events */
    private function onKeyDown () : Void
    {
        setIdleInterval();
    }
    private function onMouseMove () : Void
    {
        setIdleInterval();
    }
    
    /* Private Methods */
    private function setIdleInterval()
    {
        this.__isActive = true;
        clearInterval (this.intervalID);
        this.intervalID = setInterval (this, "broadcastIdle", this.idleTime, this);
    }
    private function broadcastIdle(watcher:IdleUserWatcher) :Void
    {
        watcher.__isActive = false;
        for (var i:Number=0; i<listeners.length; i++)
        {
            listeners*.onUserIdle();
        }
        clearInterval(watcher.intervalID);
    }
}

This an EXTREMELY simple class to use, here is what the code looks like to use it in an fla.


// here you simply tell the object how long to wait for the user
// before deciding he/she is inactive
var idleWatcher = new IdleUserWatcher(1000);

// then we register an object to listen for the onUserIdle() event from
// our IdleUserWatcher object
idleWatcher.addListener(_root);

// IdleUserWatcher has one property, it returns whether or not the user is active
trace(idleWatcher.isActive);

function onUserIdle()
{
    trace("BUZZ!!! COME BACK!!");
}

Have fun with this… I’ve officially decided that every site I do will now have a screensaver! WOOT!
_Michael

Great work!
Thanks for sharing!

No worries, post your use of it if you’d like, I"m sure we will.

_Michael

hey that code was cool. I’m actually going to use it for my next site. I’ll send you a link. thanks. I just wanted to ask if you could help my modification of two tutorials on this site. the XMl slideshow and the Xml photogallery code. located here:

http://www.kirupa.com/developer/mx2004/xml_slideshow3.htm
http://www.kirupa.com/developer/mx2004/xml_flash_photogallery.htm .

I added a stop and start slideshow function and the previous and next buttons to manually veiw images. The next button however just restarts the slideshow. I was wondering if you can show me the problem. here is the file and the code. Thanks for any help.

Rmcnaugh

delay = 3000;
//-----------------------
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
description = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
description* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
}
firstImage();
} else {
content = “file not loaded!”;
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load(“images.xml”);
/////////////////////////////////////
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
if (p == (total-1)) {
}
prevImage();
//newtxt = “SLIDESHOW OFF”;
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
newtxt = “SLIDESHOW OFF”;
}
};
Key.addListener(listen);
previous_btn.onRelease = function() {
if (p == (total-1)) {
}
prevImage();
stop();
//newtxt = “SLIDESHOW OFF”;
};
next_btn.onRelease = function() {
//slideshow() = false;
nextImage();
//newtxt = “SLIDESHOW OFF”;
};
/////////////////////////////////////
p = 0;
s = 0;
this.onEnterFrame = function() {
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 10;
}
}
};
function nextImage() {
if (p<(total-1)) {
p++;
}
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
slideshow();
}
}
function prevImage() {
if (p>0) {
p–;
picture._alpha = 0;
picture.loadMovie(image[p], 1);
desc_txt.text = description[p];
picture_num();
}
}
function firstImage() {
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[0], 1);
desc_txt.text = description[0];
picture_num();
slideshow();
}
}
function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" / "+total;
}
function slideshow() {
myInterval = setInterval(pause_slideshow, delay);
function pause_slideshow() {
clearInterval(myInterval);
if (p == (total-1)) {
p = 0;
firstImage();
} else {
nextImage();
}
}
////
stop_slideshow.onPress = function() {
clearInterval(myInterval);
stop();
newtxt = “SLIDESHOW OFF”;
};
}
///
start_slideshow.onPress = function() {
clearInterval(myInterval);
nextImage();
newtxt = “SLIDESHOW ON”;
};

:hr:

lol… make a new thread.

Has anyone got a small FLA they can post that has an example of this class… to help the uneducated (such as myself)

:cap: This kind of classes can be used for making applications which requires user presence continuously (or which has a security threat).
Take for example yahoo.
If you have opened up a yahoo mail account and don’t do anything for say 10 minutes it’s session will expire (u need to relogin). This is to keep off the ill usage of your account by some one else.
If the same yahoo thingy is to be done in flash, this class will be very useful.

So now Michael do you feel like calling yahoo???

big checks??

Honestly I’m working on a new version, I’m going to benchmarking them and see how well they run… This one has it’s drawbacks.

_Michael

I have made this already to create some sort of screensaver inside Flash movie :wink: