python h_pythonh控制

你需要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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值