In Python 3, how can I tell if Windows is locked?

后端 未结 8 742
小鲜肉
小鲜肉 2021-01-13 11:27

How can I check whether a Windows OS workstation is locked? (e.g. Win+L or choosing the lock option after Ctrl+Alt+Del.)

I want something like ctypes.windll.us

8条回答
  •  自闭症患者
    2021-01-13 12:17

    Something like this should do the trick:

    import time
    import ctypes
    
    user32 = ctypes.windll.User32
    OpenDesktop = user32.OpenDesktopA
    SwitchDesktop = user32.SwitchDesktop
    DESKTOP_SWITCHDESKTOP = 0x0100
    
    while 1:
      hDesktop = OpenDesktop ("default", 0, False, DESKTOP_SWITCHDESKTOP)
      result = SwitchDesktop (hDesktop)
      if result:
        print "Unlocked"
        time.sleep (1.0)
      else:
        print time.asctime (), "still locked"
        time.sleep (2)
    

提交回复
热议问题