开发者

Problem with the Alt key in macro recorder

开发者 https://www.devze.com 2023-03-27 02:43 出处:网络
I am writing a macro recorder (based on PyHook) and cannot make it to press key combination #2 after it hooked key combination #1, if #1 contains the Alt key. What else should I do besides unpressing

I am writing a macro recorder (based on PyHook) and cannot make it to press key combination #2 after it hooked key combination #1, if #1 contains the Alt key. What else should I do besides unpressing modifier keys? (alt, ctrl, shift)? Below are two main functions.

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


Changing code fragment from

keybd_event(KeyDict["alt"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["ctrl"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["shift"], 0, KEYEVENTF_KEYUP, 0)

to

keybd_event(KeyDict["ctrl"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["shift"], 0, KEYEVENTF_KEYUP, 0)
keybd_event(KeyDict["alt"], 0, KEYEVENTF_KEYUP, 0)

solves the problem. It's the worst sort of magic that I have ever seen!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号