pythonh控制

如何用Python来实现windows10系统的操作监控?使得电脑能在无任何操作30分钟后自动关机。主要是如何来监控?

阅读 2.9k
1 个回答

你需要pyHook这个神器, 直接放码

运行环境: python 2.7.11 (x64) & Windows 10 (x64)

# coding=utf8
import os
import threading
import time

import pyHook  # 在http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook这里下载, 用pip安装
import pythoncom  # 在https://sourceforge.net/projects/pywin32/files/pywin32/Build%20220/这里下载安装

last_time = time.time()
flag = False  # 标志位, 记录子线程开启状态

def shut_down():
    while 1:
        time.sleep(1)
        new_time = time.time()
        # print("new time: {}".format(new_time))
        if new_time - last_time > 1800:  # 30分钟无按键响应就关机
            os.system("shutdown /s /t 1")  # 1秒后关机


def OnMouseEvent(event):
    global last_time
    last_time = time.time()
    # print("old time: {}".format(last_time))
    return True


def OnKeyboardEvent(event):
    global last_time, flag
    if not flag and str(event.Key) == 'Space':  # 按下空格键启动子线程计时
        t = threading.Thread(target=shut_down)
        t.setDaemon(True)  # 设定主线程结束时自动杀掉子线程
        t.start()
        flag = True
    last_time = time.time()
    # print("old time: {}".format(last_time))
    if str(event.Key) == 'Escape':  # 按下ESC退出程序
        exit()
    # print(event.Key)
    return True


def main():
    # create the hook mananger
    hm = pyHook.HookManager()
    # register two callbacks
    hm.MouseAllButtonsDown = OnMouseEvent
    hm.KeyDown = OnKeyboardEvent
    # hook into the mouse and keyboard events
    hm.HookMouse()
    hm.HookKeyboard()
    pythoncom.PumpMessages()


if __name__ == "__main__":
    main()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏