宏录制器中的Alt键出现问题

时间:2011-08-10 03:37:35

标签: python keyboard-hook

我正在编写一个宏录制器(基于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

1 个答案:

答案 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)

解决了这个问题。这是我见过的最糟糕的魔法!