就像 Ctl,Alt + 删除
我想编写一个程序,它在 python 中使用具有 3 个或更多参数的全局热键。只有当我按下键盘上的所有三个键时,分配的功能才会执行。例如 alt、windows 和 F3。
win32con.VK_F3, win32con.MOD_WIN, win32con.VK_F5
这是我要运行的当前程序,但是它的输出是:
Traceback (most recent call last):
File "C:\Python32\Syntax\hot keys\hotkeys2.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.MOD_WIN, 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, (vk, modifiers) in HOTKEYS.items ():
print ("Registering id", id, "for key", vk)
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)
#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)")
注册 3 个热键?可能的? 解释如何使用分配一个需要按下的键,然后是否需要按下其中两个。但是,我不会认为该功能仅在同时按下所有功能时才执行。我拿了