如何在Windows上创建一个带有3个参数的全局热键?
发布时间:2020-12-13 20:32:06 所属栏目:Windows 来源:网络整理
导读:就像Ctl一样,Alt删除 我想编写一个程序,它在python中使用带有3个或更多参数的全局热键.只有当我按下键盘上的所有三个键时才能执行指定的功能.例如alt,windows和F3. win32con.VK_F3,win32con.MOD_WIN,win32con.VK_F5 这是我想要运行的当前程序,但其输出是: T
就像Ctl一样,Alt删除
我想编写一个程序,它在python中使用带有3个或更多参数的全局热键.只有当我按下键盘上的所有三个键时才能执行指定的功能.例如alt,windows和F3. win32con.VK_F3,win32con.MOD_WIN,win32con.VK_F5 这是我想要运行的当前程序,但其输出是: Traceback (most recent call last): File "C:Python32Syntaxhot keyshotkeys2.py",line 41,in <module> for id,(vk,modifiers) in HOTKEYS.items (): ValueError: too many values to unpack (expected 2) 该程序: import os import sys import ctypes from ctypes import wintypes import win32con byref = ctypes.byref user32 = ctypes.windll.user32 HOTKEYS = { 1 : (win32con.VK_F3,win32con.VK_F5),2 : (win32con.VK_F4,win32con.MOD_WIN),3 : (win32con.VK_F2,win32con.MOD_WIN) } def handle_win_f3 (): #os.startfile (os.environ['TEMP']) print ("Hello WOrld! F3") def handle_win_f4 (): #user32.PostQuitMessage (0) print ("Hello WOrld! F4") def handle_win_f1_escape (): print("exit") sys.exit() HOTKEY_ACTIONS = { 1 : handle_win_f3,2 : handle_win_f4,3 : handle_win_f1_escape } for id,modifiers) in HOTKEYS.items (): print ("Registering id",id,"for key",vk) if not user32.RegisterHotKey (None,modifiers,vk): print ("Unable to register id",id) try: msg = wintypes.MSG () while user32.GetMessageA (byref (msg),None,0) != 0: if msg.message == win32con.WM_HOTKEY: action_to_take = HOTKEY_ACTIONS.get (msg.wParam) #print(" msg.message == win32con.WM_HOTKEY:") 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) print("user32.UnregisterHotKey (None,id)") Registering 3 hotkeys? Possible?
首先,如果你想要alt,windows和F3,你不需要为HOTKEYS条目使用win32con.VK_F3,win32con.MOD_ALT,win32con.MOD_WIN吗?
但是,使用Win和F5键的修饰符按F3并不是很有意义. 行上的错误: for id,modifiers) in HOTKEYS.items (): 是因为每个字典条目的值是一个可变长度元组.这是一种处理方法,它也将所有修饰符值按位或一起准备将它们作为单个参数传递给RegisterHotKey(). from functools import reduce for id,values in HOTKEYS.items (): vk,modifiers = values[0],reduce (lambda x,y: x | y,values[1:]) print ("Registering id",vk) if not user32.RegisterHotKey (None,vk): print ("Unable to register id",id) 如果您的代码正确缩进并遵循PEP 8 — Style Guide for Python Code建议,那么解决您的问题会更容易.请考虑将来这样做. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows CMD命令大全
- _asm int 3 软件中断
- .net – 移动设备上的OutOfMemoryException
- windows – 委派注销权限?
- windows-server-2008 – IIS7 Windows Server 2008 FTP –
- .net – 使用Microsoft.Web.Administration远程管理IIS时的
- 是否可以使用R命令在Windows上安装pandoc?
- windows-server-2012-r2 – 应用dotdefender ISAPI过滤器导
- windows-server-2003 – IIS6,Windows 2003 Server上的Ioni
- Microsoft Office Excel