Tell if Win2000 or XP is inactive

Richie Hindle richie at entrian.com
Thu Jul 15 11:27:53 EDT 2004


[Richie]
> Would you be prepared to install ctypes and one extra DLL?

Ah!  I hadn't spotted that you were running on Win2000+.  In that case you
can do it with ctypes alone:

---------------------------------------------------------------------------

import time
from ctypes import *

GetTickCount = windll.kernel32.GetTickCount
GetLastInputInfo = windll.user32.GetLastInputInfo

class LASTINPUTINFO(Structure):
    _fields_ = [("cbSize", c_uint),
                ("dwTime", c_uint)]

lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
for i in range(10):
    GetLastInputInfo(byref(lastInputInfo))
    idleDelta = float(GetTickCount() - lastInputInfo.dwTime) / 1000
    print "Last input event was %.2f seconds ago." % idleDelta
    time.sleep(1)

---------------------------------------------------------------------------

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list