就像Ctl,Alt +刪除如何使用3個參數在Windows上創建全局熱鍵?
我想寫一個程序,它使用全局熱鍵和3個或更多的參數在Python中。只有當我按下鍵盤上的所有三個鍵時才能執行指定的功能。例如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)")
Registering 3 hotkeys? Possible? 介紹一個如何使用分配一個密鑰需要按下,然後如果其中兩個需要按下。不過,我不會說只有同時按下所有按鈕才能執行此功能。我花了
https://github.com/boppreh/keyboard – Andrew