I'm developing a chat application with appjs that uses node.js as the platform. I'm stucked with detecting when computer is idle (when user is away from it or not using it).
There is os module in node.js and its os.cpus()
provides such information for each core:
[ { model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 252020,
nice: 0,
sys: 30340,
idle: 1070356870,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 306960,
nice: 0,
sys: 26980,
idle: 1071569080,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 248450,
nice: 0,
sys: 21750,
idle: 1070919370,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 256880,
nice: 0,
sys: 19430,
idle: 1070905480,
irq: 20 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 511580,
nice: 20,
sys: 40900,
idle: 1070842510,
irq: 0 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 291660,
nice: 0,
sys: 34360,
idle: 1070888000,
irq: 10 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 308260,
nice: 0,
sys: 55410,
idle: 1071129970,
irq: 880 } },
{ model: 'Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz',
speed: 2926,
times:
{ user: 266450,
nice: 1480,
sys: 34920,
idle: 1072572010,
irq: 30 } }
]
So does it fit for detecting user idle? As I understand there are two values which I can use: user
and idle
. The idle
value iterates very quickly but the user
one iterates in chaotic way. What I'm looking for is to know when user is not moving mouse or typing in ANY application (not only in my app) and after some threshold timeout (for example 60 seconds) of inactivity I need to change his status to 'away' and when he's back change it back 'to online'. Could you please point me to some alghorithm how to do that or even drop some code example for me?
Thanks in advance.
Edit. As I know each OS has API to detect user is idle and for example Adobe Air as platform has ability to do that easily and I know I can use node-ffi or even write a module. Furthermore as I know Chromium also has this ability out of the box.
According to os.cpus() documentation :
So the
times
field shows all the time spent on a cpu core and how it was used since startup. But this is not what you are looking for because the output details CPU usage for system not for appjs window in particular.Now how to find out your appjs application is active/idle. You should check the appjs page. There are some events like
mousemove
andkeydown
which you can use in you application.