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

后端 未结 8 775
小鲜肉
小鲜肉 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:13

    Based on @Stardidi answer, this worked for me (Windows 10 Pro):

    import time
    import win32gui
    import win32api
    import win32con
    import win32process
    
    while True:
        time.sleep(1)
        _, pid = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())
    
        try:
            handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
            filename = win32process.GetModuleFileNameEx(handle, 0)
        except Exception as _e:
            filename = "LockApp.exe"
            del _e
    
        current_status = "locked" if "LockApp" in filename else "unlocked"
    

提交回复
热议问题