Skip to content

Instantly share code, notes, and snippets.

@jamesadney
Forked from tcurvelo/energysaver.py
Created February 22, 2012 06:43
Show Gist options
  • Save jamesadney/1882516 to your computer and use it in GitHub Desktop.
Save jamesadney/1882516 to your computer and use it in GitHub Desktop.
idle time on windows
#!/usr/bin/env python
from ctypes import Structure, windll, c_uint, sizeof, byref
import time
class LASTINPUTINFO(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]
def get_idle_duration():
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
windll.user32.GetLastInputInfo(byref(lastInputInfo))
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
return millis / 1000.0
while True:
time.sleep (2.0)
idleness=get_idle_duration()
if idleness> 10:
print "away (%d sec w/o iteract)"%idleness
else:
print "active (%d sec w/o iteract)"%idleness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment