[Tutor] How would I make a program that password protects on aninactivity timer?

Bill Burns billburns at pennswoods.net
Tue Aug 29 21:47:40 CEST 2006

[Alan Gauld]
> I'd try the screen saver settings first, its much easier!

As Alan has stated above, the screen saver idea is probably the easiest
way to go, but personally... I always to do it the the hard way :-)

Using ideas from eariler posts (regarding Windows events and ctypes),
here's my attempt.

Warning: This is an initial start that you can fix-up and build upon.

Suggestions welcome.


Locks a Windows WorkStation if a user is idle for a specific
amount of time.

My definition of idle is:
1). No keys have been pressed.
2). The mouse has not been moved.

To run this, you will need to install:
pywin32     -> http://sourceforge.net/projects/pywin32/
ctypes      -> http://sourceforge.net/projects/ctypes/
pyHook      -> http://www.cs.unc.edu/Research/assist/developer.shtml

This works for me on Windows 2000 Pro SP4 using:
Python 2.4.3
pywin32 Build 208

Things that should probably be fixed:
1). This only runs once. When we hit the limit in idleTimer(), we
break out of the loop and never go back. Ideally, it should reset /
restart when we logon again...
2). Do we need a method to stop the message pump? Right now, it
just runs indefinitely.

import time
from threading import Thread
import Queue

import pythoncom
import ctypes
import win32api
import pyHook

def idleTimer(limit, queue):
     # Count how long we are idle by incrementing our
     # queue. The mouse and keyboard events will 'reset'
     # the queue to zero. If the limit is hit, we lock
     # the WorkStation.
     while True:
         idleTime = queue.get_nowait()
         print "We've been idle for %s second(s)." % (idleTime)
         if idleTime == limit:
             # Lock the WorkStation.
         queue.put(idleTime + 1)

def mouseEvent(event):
     # If the mouse moves, this get fired.
     if event.MessageName == 'mouse move':
             # The mouse was moved, so 'reset' the timer.
             # Note: When the mouse is moving, this function
             # gets hit 'a lot'... Initially, I was only
             # doing queue.put(0), but that just clobbered
             # the queue with zeros. To combat that, I do
             # a get() then a put(). There's gotta be a
             # better way....
             # We don't want to stop these events, so we
             # need to return True. Don't change this!
             return True
         except Queue.Empty:
             # Again, don't change this!
             return True

def keyboardEvent(event):
     # Same as above except for key presses.
     if event.MessageName == 'key down':
             return True
         except Queue.Empty:
             return True

if __name__ == '__main__':
     # Set the idle limit (in seconds).
     idleLimit = 10
     # Make a queue.
     queue = Queue.Queue()
     # Put an initial value into the queue.
     # Fire-up our idle timer.
     timer = Thread(target=idleTimer, args=(idleLimit, queue))
     # Hook both the mouse and keyboard events.
     # Create the hook manager.
     hm = pyHook.HookManager()
     # Watch for these events.
     hm.MouseAll = mouseEvent
     hm.KeyDown = keyboardEvent
     # Set the hooks.
     # Wait forever.


More information about the Tutor mailing list