native-idle-time queries the native windowing system (Windows, X, macOS) for the user's idle time, providing the number of milliseconds the user has been idle. The idle time is the same that is used to launch screensavers.
npm install native-idle-time
npm
will try to install a binary version of the addon from Github. If a
binary does not exist for your platform, npm
will try to build the addon.
Installing in Linux may require libxss-dev
and npm package nan
. If
you are trying to build the addon and it fails with the error message:
../native-idle-time_linux.cc:10:38: fatal error: X11/extensions/scrnsaver.h: No such file or directory
then install libxss-dev
with the following:
sudo apt-get install libxss-dev
If you get the error:
Error: Cannot find module 'nan'
then use npm
to install nan
with:
npm install nan
const idleTime = require('native-idle-time')
setTimeout(() => {
var millis = idleTime.getMillis()
console.log(`User idle time: ${millis}ms`)
}, 1000)
Example: let millis = native_idle_time.getMillis()
Returns the time, in milliseconds, the system has gone without user input. If
the addon cannot determine the time (no DISPLAY, ssh session, etc.), -1
is returned.