Q:

Detect when computer is unlocked Windows

I have used this excellent method to lock a windows computer; that part works fine.  What I would like to do, however, is detect when the computer is then unlocked and then proceed with my program.  I have done a fair bit of research on the web to find a way to do this, but I haven't seen any way for python to detect a change in the computer status (ie when the computer is logged on again or unlocked).  Is it possible in python to detect when the Windows machine is unlocked or logged on to?  Note I am working with python 2.7.6 and on Windows 7.  Thanks

A:

I found a script in http://timgolden.me.uk/python/win32_how_do_i/see_if_my_workstation_is_locked.html 
import time
import ctypes

user32 = ctypes.windll.User32
OpenDesktop = user32.OpenDesktopA
SwitchDesktop = user32.SwitchDesktop
DESKTOP_SWITCHDESKTOP = 0x0100

user32.LockWorkStation ()
#
# Slight pause to overcome what appears to
# be a grace period during which a switch
# *will* succeed.
#
time.sleep (1.0)

while 1:
  hDesktop = OpenDesktop ("default", 0, False, DESKTOP_SWITCHDESKTOP)
  result = SwitchDesktop (hDesktop)
  if result:
    print "Unlocked"
    break
  else:
    print time.asctime (), "still locked"
    time.sleep (2)

