我正在编写一个宏录制器(基于PyHook),如果#1包含Alt键,则在挂钩组合键#1之后无法按键组合#2。除了禁止修改键之外,我还应该做些什么呢? (alt,ctrl,shift)?以下是两个主要功能。
def press_key(Key, gModifiers = []):
keybd_event(KeyDict["alt"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["ctrl"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["shift"], 0, KEYEVENTF_KEYUP, 0)
for Modifier in gModifiers:
keybd_event(KeyDict[Modifier], 0, 0, 0)
keybd_event(KeyDict[Key], 0, 0, 0)
keybd_event(KeyDict[Key], 0, KEYEVENTF_KEYUP, 0)
for Modifier in reversed(gModifiers):
keybd_event(KeyDict[Modifier], 0, KEYEVENTF_KEYUP, 0)
def OnKeyboardEvent(Event):
global HM, Mode, PressedKeys, PrevKeyName
if Mode == "play":
return True
PyHookKeyName = Event.Key
IsAppend = 1
if PyHookKeyName in PyHookDict:
KeyName = PyHookDict[PyHookKeyName]
if is_modifier(KeyName) and KeyName == PrevKeyName:
IsAppend = 0
elif len(PyHookKeyName) > 1 and PyHookKeyName[0] == "F": # F1 .. F12
KeyName = PyHookKeyName
else:
KeyName = Event.Key
if Mode == "record" and IsAppend:
append(PressedKeys, KeyName)
if not is_modifier(PrevKeyName):
AppsPressed = 0
WinPressed = 0
if KeyName == "apps":
AppsPressed = 1
elif KeyName == "win":
WinPressed = 1
if Mode == "hook" and KeyName in MacrosKeysDict:
Mode = "play"
PressedModifiersSum = pressed_modifiers_sum()
if PressedModifiersSum in MacrosKeysDict[KeyName]:
MacroBlock = MacrosDict[(KeyName, PressedModifiersSum)]
Events = MacroBlock[2]
for Event in Events:
press_key(Event[0], Event[1])
Mode = "hook"
PrevKeyName = KeyName
# return True to pass the event to other handlers
return True
答案 0 :(得分:0)
从
更改代码片段keybd_event(KeyDict["alt"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["ctrl"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["shift"], 0, KEYEVENTF_KEYUP, 0)
到
keybd_event(KeyDict["ctrl"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["shift"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["alt"], 0, KEYEVENTF_KEYUP, 0)
解决了这个问题。这是我见过的最糟糕的魔法!