5

How can I see for how long there hasn't been any input via mouse or keyboard on my computer?

Jacob Vlijm
  • 85,475
Beili
  • 53

1 Answers1

7

How to see idle time; the time your computer has no input from mouse or keyboard

  • To see idle time, you'll need xprintidle:

    sudo apt-get install xprintidle
    
  • To show what xprintidle does, run:

    sleep 5 && xprintidle
    

    enter image description here

    ...which shows idle time in milliseconds

  • If you continuously want to show the idle time in terminal:

    while true; do xprintidle && sleep 1; done
    

    enter image description here

Off course, xprintidle can be used in scripts to offer a more elegant way to keep an eye on idle time, but this is basically it.

Just for fun

...Since we do everything with an indicator these days, an example how to show idle time in the GUI, using xprintidle:

enter image description here

#!/usr/bin/env python3
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
import time
from threading import Thread
import os
import subprocess

currpath = os.path.dirname(os.path.realpath(__file__))

class Indicator():
    def __init__(self):
        self.app = 'show_idlet'
        iconpath = os.path.join(currpath, "idle.png")
        self.indicator = AppIndicator3.Indicator.new(
            self.app, iconpath,
            AppIndicator3.IndicatorCategory.OTHER)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
        self.indicator.set_menu(self.set_menu())
        self.timer = Thread(target=self.check_recent)
        self.timer.setDaemon(True)
        self.timer.start()

    def set_menu(self):
        # quit
        menu = Gtk.Menu()
        item_quit = Gtk.MenuItem('Quit')
        item_quit.connect('activate', self.stop)
        menu.append(item_quit)
        menu.show_all()
        return menu

    def showtime(self, section):
        return (2-len(str(section)))*"0"+str(section)

    def convert_time(self, time):
        hrs = self.showtime(str(int(time/3600)))
        mins = self.showtime(str(int((time%3600)/60)))
        secs = self.showtime(str(time%60))
        return hrs+":"+mins+":"+secs

    def check_recent(self):
        while True:
            time.sleep(1)
            idle = round(int(subprocess.check_output(
                ["xprintidle"]).decode("utf-8").strip())/1000)
            GObject.idle_add(
                self.indicator.set_label,
                self.convert_time(idle), self.app,
                priority=GObject.PRIORITY_DEFAULT
                )

    def stop(self, source):
        Gtk.main_quit()

Indicator()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

How to use

  1. Install xprintidle

    sudo apt-get install xprintidle
    
  2. Copy the script above into an empty file, save it as show_idle.py

  3. Copy the icon below (right-click > save as), save it as (exactly named): idle.png, in one and the same directory as the script

    enter image description here

  4. Run the indicator from a terminal:

    python3 /path/to/show_idle.py
    
  5. If you want, you can add it to startup applications: choose Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 10 && python3 /path/to/show_idle.py"
    
Jacob Vlijm
  • 85,475