How to detect desktop idle time from an Electron app?

大兔子大兔子 提交于 2019-12-03 07:48:08

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

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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!