python - 检测计算机何时解锁 Windows

标签 python windows python-2.7

我用过this excellent method锁定 Windows 计算机;那部分工作正常。然而,我想做的是检测计算机何时解锁,然后继续执行我的程序。我在网上做了相当多的研究来找到一种方法来做到这一点,但我还没有看到任何方法让 python 检测计算机状态的变化(即当计算机再次登录或解锁时)。 Python 是否可以检测 Windows 计算机何时解锁或登录?注意我正在使用 python 2.7.6 和 Windows 7。谢谢

最佳答案

我在 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)

关于python - 检测计算机何时解锁 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599462/

相关文章:

python - 如何在装饰器记录器中获取 flask 的请求?

c++ - 我可以从线程内调用 GetThreadTimes() 吗?

python - 将所有 Python 解释器错误转储到日志中?

python - 将标签(来自字符串列表)合并在一起

windows - .bat 中的 IF、CALL、EXIT 和 %ERRORLEVEL%

c - MinGW - 为什么 C 'hello world' 可执行文件有 42kb?

python - 反转数据框中的行值

python - 将 unhexlify 从 Python 2.7 移植到 Python 3.5

python - 删除长子命令帮助输出?

python - 在 Python 中重命名多个文件