python注册热键方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #!/usr/bin/env python3 import win32con import ctypes import ctypes.wintypes from threading import Thread,activeCount, enumerate from time import sleep,time class Hotkey(Thread): user32 = ctypes.windll.user32 hkey_list = {} hkey_flags = {} #按下 hkey_running = {} #启停 _reg_list = {} #待注册热键信息 def regiskey( self , hwnd = None , flagid = 0 , fnkey = win32con.MOD_ALT, vkey = win32con.VK_F9): # 注册热键,默认一个alt+F9 return self .user32.RegisterHotKey(hwnd, flagid, fnkey, vkey) def get_reginfo( self ): return self ._reg_list def get_id( self ,func): self_id = None for id in self .get_reginfo(): if self .get_reginfo()[ id ][ "func" ] = = func: self_id = id break if self_id: self .hkey_running[self_id] = True return self_id def get_running_state( self ,self_id): if self .hkey_running.get(self_id): return self .hkey_running[self_id] else : return False def reg( self ,key,func,args = None ): id = int ( str ( round (time() * 10 ))[ - 6 :]) fnkey = key[ 0 ] vkey = key[ 1 ] info = { "fnkey" :fnkey, "vkey" :vkey, "func" :func, "args" :args } self ._reg_list[ id ] = info # print(info) #这里待注册的信息 sleep( 0.1 ) return id def fast_reg( self , id ,key = ( 0 ,win32con.VK_HOME),func = lambda : print ( '热键注册开始' )): if not self .regiskey( None , id , key[ 0 ], key[ 1 ]): print ( "热键注册失败" ) return None self .hkey_list[ id ] = func self .hkey_flags[ id ] = False return id def callback( self ): def inner( self = self ): for flag in self .hkey_flags: self .hkey_flags[flag] = False while True : for id , func in self .hkey_list.items(): if self .hkey_flags[ id ]: args = self ._reg_list[ id ][ "args" ] if args: # print(args) #这里打印传入给注册函数的参数 thread_it(func, * args) else : thread_it(func) self .hkey_flags[ id ] = False return inner def run( self ): for id in self ._reg_list: reg_info = self ._reg_list[ id ] fnkey = reg_info[ "fnkey" ] vkey = reg_info[ "vkey" ] func = reg_info[ "func" ] self .fast_reg( id ,(fnkey, vkey), func) fn = self .callback() thread_it(fn) # 启动监听热键按下线程 try : msg = ctypes.wintypes.MSG() while True : if self .user32.GetMessageA(ctypes.byref(msg), None , 0 , 0 ) ! = 0 : if msg.message = = win32con.WM_HOTKEY: if msg.wParam in self .hkey_list: self .hkey_flags[msg.wParam] = True self .user32.TranslateMessage(ctypes.byref(msg)) self .user32.DispatchMessageA(ctypes.byref(msg)) finally : for id in self .hkey_list: self .user32.UnregisterHotKey( None , id ) def thread_it(func, * args): t = Thread(target = func, args = args) t.setDaemon( True ) t.start() def jump(func,hotkey): self_id = hotkey.get_id(func) while hotkey.get_running_state(self_id): print (f "{self_id : } 你正在1秒1次的跳动" ) sleep( 1 ) def stop_jump(start_id,hotkey): hotkey.hkey_running[start_id] = False print (f "{start_id} 即将停止" ) sleep( 1 ) print (f '当前线程列表:{activeCount()}' , enumerate ()) def main(): hotkey = Hotkey() start_id = hotkey.reg(key = (win32con.MOD_ALT,win32con.VK_HOME),func = jump,args = (jump,hotkey)) #alt home键 开始 hotkey.reg(key = ( 0 ,win32con.VK_END),func = stop_jump,args = (start_id,hotkey)) #alt end键 结束 hotkey.start() #启动热键主线程 print (f "当前总线程数量:{activeCount()}" ) print ( '当前线程列表:' , enumerate ()) print ( '热键注册初始化完毕,尝试按组合键alt+Home 或者单键END看效果' ) if __name__ = = '__main__' : main() |
以下是旧的代码,用起来比较麻烦。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/usr/bin/env python3 # _*_ coding: utf-8 _*_ # File : demo.py # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------ # Date : 2019/6/28 import win32con import ctypes import ctypes.wintypes from threading import Thread, Timer, activeCount, enumerate from time import sleep h_ids = [i for i in range ( 2 )] # 创建两个热键序列 h_keys = {i: False for i in h_ids} # 初始化所有热键序列的标志符为False h_dict = {} # 初始化一个空的字典,记录id与func class Hotkey(Thread): # 创建一个Thread的扩展类 user32 = ctypes.windll.user32 # 加载user32.dll # global h_ids, h_keys,h_dict def regiskey( self , hwnd = None , flagid = 0 , fnkey = win32con.MOD_ALT, vkey = win32con.VK_F9): # 注册热键,默认一个alt+F9 return self .user32.RegisterHotKey(hwnd, flagid, fnkey, vkey) def callback( self , id , func): h_dict[ id ] = func # 这个id对应这个func,没有就是新增,有就是修改 def inner(): for key, value in h_dict.items(): print (f '总的热键池:{h_ids},当前热键序号:{key}, 当前热键功能:{value},当前热键状态:{h_keys[h_ids[key]]}' ) while True : for key, value in h_dict.items(): if h_keys[h_ids[key]]: thread_it(value) # 另外开线程执行value h_keys[h_ids[key]] = False return inner def run( self ): # print(self.user32) if not self .regiskey( None ,h_ids[ 0 ],win32con.MOD_ALT,win32con.VK_F9): # 注册快捷键alt+F9并判断是否成功,该热键用于执行一次需要执行的内容。 print (f "热键注册失败! id{h_ids[0]}" ) # 返回一个错误信息 if not self .regiskey( None ,h_ids[ 1 ], 0 ,win32con.VK_F10): # 注册快捷键F10并判断是否成功,该热键用于结束程序,且最好这么结束,否则影响下一次注册热键。 print (f "热键注册失败! id{h_ids[1]}" ) # 以下为检测热键是否被按下,并在最后释放快捷键 try : msg = ctypes.wintypes.MSG() while True : if self .user32.GetMessageA(ctypes.byref(msg), None , 0 , 0 ) ! = 0 : if msg.message = = win32con.WM_HOTKEY: if msg.wParam in h_ids: h_keys[msg.wParam] = True self .user32.TranslateMessage(ctypes.byref(msg)) self .user32.DispatchMessageA(ctypes.byref(msg)) finally : for i in h_ids: self .user32.UnregisterHotKey( None , i) # 必须得释放热键,否则下次就会注册失败,所以当程序异常退出,没有释放热键, # 那么下次很可能就没办法注册成功了,这时可以换一个热键测试 def thread_it(func, * args): t = Thread(target = func, args = args) t.setDaemon( True ) t.start() def settimeout(func, sec): def inner(): func() Timer(sec, inner).start() thread_it(inner) def setinterval(func, sec, tmrname, flag = True ): global timer_dict timer_dict[tmrname] = flag print ( "已设置tqtimer启用状态为:{}" . format (flag)) def inner(): global timer_dict if timer_dict[tmrname]: func() Timer(sec, inner).start() thread_it(inner) def clearinterval(timername): global timer_dict timer_dict[timername] = False flag = timer_dict[timername] print ( "已设置tqtimer启用状态为:{}" . format (flag)) def test_start(): print ( "按下了开始键...the programe is running" ) def test_stop(): print ( "按下了停止键...the programe is stopped" ) def run_ok(): hotkey = Hotkey() hotkey.start() fn = hotkey.callback( 0 , test_start) fn = hotkey.callback( 1 , test_stop) thread_it(fn) sleep( 0.5 ) count = activeCount() print (f "当前总线程数量:{count}" ) print ( '当前线程列表:' , enumerate ()) print ( '热键注册初始化完毕,尝试按组合键alt+F9 或者单键F10看效果' ) while True : pass if __name__ = = '__main__' : run_ok() |
关于使用python3怎么注册一个全局热键就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了