Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Detecting idle time on Mac

by sgifford (Prior)
on Sep 14, 2006 at 03:36 UTC ( #572849=perlquestion: print w/replies, xml ) Need Help??

sgifford has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a Perl application that, for various reasons, needs to know how long it's been since the user has been actively using the computer, that is, how long since the last mouse or keyboard activity. I can detect this on Unix with X11::IdleTime and by looking at tty mtime's, and I found a node about detecting idle time on Windows. But I haven't found a way to do this on Mac yet.

Any ideas?


Replies are listed 'Best First'.
Re: Detecting idle time on Mac
by Fletch (Chancellor) on Sep 14, 2006 at 12:31 UTC

    Google is your friend.

    ioreg -c IOHIDSystem | perl -lane 'if(/Idle/) {print $F[-1]/1000000000 +; last;}'

    Update: cleaned up that link's Perl version a bit.

      Thanks Fletch, looks like that will work! For some reason I didn't think to search for OS X; I had been searching for Mac keyboard idle time and similar, which strangely turned up nothing.

      Does anybody happen to know of a way to get this information without forking a copy of ioreg?


        There should be some way to access it through C and/or ObjectiveC, but exactly how is a different question. The Apple Developer Site has a pretty good set of documentation; you'll probably want to start looking here for stuff on I/O Kit and the HID related frameworks.

        Unless this is something that's going to poll constantly I'd just cheat and run ioreg; it only takes six hundredths of a second to run according to my shell's timeing.

Re: Detecting idle time on Mac
by borisz (Canon) on Sep 14, 2006 at 08:54 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://572849]
Approved by friedo
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2021-01-23 15:11 GMT
Find Nodes?
    Voting Booth?