Python:通过自定义系统级快捷键来控制程序运行

最后更新于:2022-04-01 19:40:13

晚上学习了下python定义系统级快捷键的方法,这里说的系统级快捷键指的是,如果当前程序窗口不为激活窗口,也能收到快捷键消息,实现的是一个控制键盘和鼠标监听的程序,如,按下Ctrl + F3,程序开始记录监控日志。本来想实现按下Ctrl + F4程序程序停止记录日志,但测试时遇到了一个没有目前还没有想到解决办法的问题,先看实现。 ### 一、代码: ~~~ #!/usr/bin/env python # -*- coding: utf-8 -*- import pythoncom import pyHook import time import pyhk import os import sys import ctypes from ctypes import wintypes import win32con class CInspectKeyAndMouseEvent: ''' Function:键盘和鼠标监控类 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-09 ''' def __init__(self, filename): self.filename = filename def open_file(self): self.fobj = open(self.filename, 'w') def close_file(self): self.fobj.close() def onMouseEvent(self, event): "处理鼠标事件" self.fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n') self.fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) self.fobj.writelines("MessageName:%s\n" % str(event.MessageName)) self.fobj.writelines("Message:%d\n" % event.Message) self.fobj.writelines("Time_sec:%d\n" % event.Time) self.fobj.writelines("Window:%s\n" % str(event.Window)) self.fobj.writelines("WindowName:%s\n" % str(event.WindowName)) self.fobj.writelines("Position:%s\n" % str(event.Position)) self.fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n') return True def onKeyboardEvent(self, event): "处理键盘事件" self.fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n') self.fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) self.fobj.writelines("MessageName:%s\n" % str(event.MessageName)) self.fobj.writelines("Message:%d\n" % event.Message) self.fobj.writelines("Time:%d\n" % event.Time) self.fobj.writelines("Window:%s\n" % str(event.Window)) self.fobj.writelines("WindowName:%s\n" % str(event.WindowName)) self.fobj.writelines("Ascii_code: %d\n" % event.Ascii) self.fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii)) self.fobj.writelines("Key:%s\n" % str(event.Key)) self.fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n') return True def InspectKeyAndMouseEvent(bRunTag = False): "" if bRunTag: my_event = CInspectKeyAndMouseEvent("D:\\hook_log.txt") my_event.open_file() #创建hook句柄 hm = pyHook.HookManager() #监控键盘 hm.KeyDown = my_event.onKeyboardEvent hm.HookKeyboard() #监控鼠标 hm.MouseAll = my_event.onMouseEvent hm.HookMouse() #循环获取消息 pythoncom.PumpMessages() my_event.close_file() else: #程序走不到这里,直接在PumpMessages处不停地监听 os._exit(0) def handle_start_InspecEvent(): "开始监控(按下Ctrl + F3)" InspectKeyAndMouseEvent(True) def handle_stop_InspecEvent(): "停止监控 (按下Ctrl + F4)" InspectKeyAndMouseEvent(False) if __name__ == "__main__": ''' Function:通过快捷键控制程序运行 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-09 ''' byref = ctypes.byref user32 = ctypes.windll.user32 #定义快捷键 HOTKEYS = { 1 : (win32con.VK_F3, win32con.MOD_CONTROL), 2 : (win32con.VK_F4, win32con.MOD_CONTROL) } #快捷键对应的驱动函数 HOTKEY_ACTIONS = { 1 : handle_start_InspecEvent, 2 : handle_stop_InspecEvent } #注册快捷键 for id, (vk, modifiers) in HOTKEYS.items (): if not user32.RegisterHotKey (None, id, modifiers, vk): print "Unable to register id", id #启动监听 try: msg = wintypes.MSG () while user32.GetMessageA (byref (msg), None, 0, 0) != 0: if msg.message == win32con.WM_HOTKEY: action_to_take = HOTKEY_ACTIONS.get (msg.wParam) if action_to_take: action_to_take () user32.TranslateMessage (byref (msg)) user32.DispatchMessageA (byref (msg)) finally: for id in HOTKEYS.keys (): user32.UnregisterHotKey (None, id) ~~~ ### 二、测试 按下Ctrl + F3后,程序开始记录日志,正常。 按下Ctrl + F4后,程序并没有停止记录日志,异常。因为在记录日志时,启动了pythoncom.PumpMessages(),不停地循环来获取消息,程序不能继续往下走。目前还没有想到解决方案,明天接着想想看有没有什么好的方法。
';