Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect desktop idle time from an Electron app?

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?

like image 409
GJ. Avatar asked Oct 28 '15 18:10

GJ.


People also ask

Is Electron good for desktop apps?

With an improved runtime and great integration with JavaScript and Node. js, Electron JS makes both designing desktop apps and maintaining them on cross platforms easier and better.

How do you tell if an app is based on Electron?

If you see an app. asar file, or something similar with the . asar suffix, it is most likely an Electron App. Windows: Open up the program files directory of the application you are wondering about, and check the file folder for any file with .


2 Answers

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

like image 163
bithavoc Avatar answered Oct 16 '22 04:10

bithavoc


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.

like image 29
Fred Antell Avatar answered Oct 16 '22 04:10

Fred Antell