How to detect desktop idle time from an Electron app?
Asked Answered
L

5

11

I need my Electron app to respond to the user becoming idle (no mouse or keyboard inputs to any program on the OS) for a certain amount of time.

How can I trigger a function based on such idle time data?

Lunn answered 28/10, 2015 at 18:8 Comment(0)
A
9

You can always detect idle times on Linux by using XScreenServer, GetLastInputInfo on Windows and CGEventSourceSecondsSinceLastEventType on Mac

I've published desktop-idle using these API's, you can check the source code https://github.com/bithavoc/node-desktop-idle

UPDATE:

Electron 3 users can use the power monitor API to achieve the same goal: https://electronjs.org/docs/api/power-monitor

Aculeate answered 7/12, 2017 at 9:12 Comment(6)
I found this one difficult to install.Eulaliaeulaliah
@Eulaliaeulaliah if you're using electron 3 you can user the power monitor for the same purpose: electronjs.org/docs/api/power-monitorAculeate
Thank you so much, this is way better!Eulaliaeulaliah
@Eulaliaeulaliah Does anyone have some good examples of how to use this? I'm just trying to use powerMonitor.getSystemIdleTime and I'm getting errors like "getSystemIdleTime is not a function" etc. There doesn't seem to be any project examples using this so it would be helpful if you could share some code.Expiatory
@jih you using Electron 3 and above ?Aculeate
@Expiatory I am getting the same error. Did you find a solution yet?Heloise
A
5

Some people have written node libraries that hook into the native platform code for OSX, Windows, and Linux to accomplish this.

I ended up using this library to accomplish the same thing in my electron app: https://github.com/paulcbetts/node-system-idle-time

It's published on npm as @paulcbetts/system-idle-time

I tested it on OSX and it seemed to work fine there.

I did originally get a "module version mismatch expected 50 got 46" error, but running this command cleared it up:

npm rebuild --runtime=electron --target=1.4.3 --disturl=https://atom.io/download/atom-shell --abi=49

Replace target with whatever version of electron you're using.

Aruwimi answered 10/10, 2016 at 1:3 Comment(0)
U
4

Since electron uses node, you should checkout RobotJS. These things are platform specific so it does need some other dependancies but you can monitor mouse/keyboard and see if it's changed, or control it (hence the name).

Untrimmed answered 1/11, 2015 at 16:8 Comment(0)
A
4

Look at Electron Power monitor API's and use

powerMonitor.getSystemIdleTime()
Aramaic answered 14/9, 2020 at 7:48 Comment(0)
C
0
//declare these global variables in electron.js
let isAppInForeGround = false;
let appInActiveTime = new Date();

then listen to these events
mainWindow.addListener('blur', () => {
//app is kept in background , capture the time at which the app went into background
        isAppInForeGround = false;
        appInActiveTime = new Date();
});

mainWindow.addListener('focus', () => {
//app is bought back to foreground 
        isAppInForeGround = true;
});

And call this below method from a timer from your electorn.js, use this method to get the app inactive duration..

const getAppInactiveDurationInSec = () => {
    let appIdleTime;
    if(isAppInForeGround) {
        //if the application is in foreground use system idle time, it will also count all the mouse events and every event .. powerMonitor.getSystemIdleTime() will listen to all the events happening in our system from different applications, so when the electron application kept in background and if you call this getSystemIdleTime method it still returns 0 if you are working on any other different application
        appIdleTime = powerMonitor.getSystemIdleTime();
    } else {
        //and if the application is in background just compare the last acive time 
     with current time.
        appIdleTime = (new Date().getTime() - appInActiveTime.getTime()) / 1000;
    }

    return appIdleTime;
}
Cathycathyleen answered 13/2 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.