Skip to content

Instantly share code, notes, and snippets.

@cpave3
Created February 11, 2019 21:50
Show Gist options
  • Save cpave3/7bc4f9a99397b8c7db764d0d9596b787 to your computer and use it in GitHub Desktop.
Save cpave3/7bc4f9a99397b8c7db764d0d9596b787 to your computer and use it in GitHub Desktop.
Determine when a user should be considered inactive or away
const ioHook = require('iohook'); // For checking system input activity
const logUpdate = require('log-update'); // For writing out to the terminal
const moment = require('moment'); // Time related stuff
let idleTime = moment();
// Listen for all the events we consider activity
ioHook.on("keyup", event => {
resetIdle();
});
ioHook.on("mousemove", event => {
resetIdle();
});
ioHook.start();
// Every second, check if they should be considered inactive yet
setInterval(() => {
logUpdate(`Time since activity: ${moment().diff(idleTime, 'seconds')} \nInactive: ${moment().diff(idleTime, 'seconds') >= 15}`)
}, 1000)
// Set the latest activity time to now
function resetIdle() {
idleTime = moment()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment