Skip to content

Instantly share code, notes, and snippets.

@tcurvelo
Last active December 28, 2016 17:54
Show Gist options
  • Save tcurvelo/764481 to your computer and use it in GitHub Desktop.
Save tcurvelo/764481 to your computer and use it in GitHub Desktop.
idle time on windows + turn monitor off
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from ctypes import Structure, windll, c_uint, sizeof, byref
from threading import Thread
import time, win32api, win32con
class LastInputInfo(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]
class Ociometro(Thread):
def __init__ (self, ocio_max=10, cad=5.0):
self.ocio_total=0
Thread.__init__(self)
self.lastInputInfo = LastInputInfo()
self.cadencia = cad
self.ocio_maximo = ocio_max
def get_ociosidade(self):
"""Retorna o tempo em segundos da ultima interação do usuario"""
self.lastInputInfo.cbSize = sizeof(self.lastInputInfo)
windll.user32.GetLastInputInfo(byref(self.lastInputInfo))
return (windll.kernel32.GetTickCount() - self.lastInputInfo.dwTime) / 1000.0
def run(self):
"""Metodo principal, que fica verificando o tempo de ociosidade
do usuario e, caso exceda o maximo configurado, o monitor será
desligado"""
while True:
time.sleep(self.cadencia)
self.ocio = self.get_ociosidade()
if self.ocio > self.ocio_maximo:
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
if __name__ == "main":
o = Ociometro()
o.start()
#!/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