moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

Flipping around example code breaks execution #482

Closed SpecialCharacter closed 2 years ago

SpecialCharacter commented 2 years ago

Description I took the example code and flipped it around. No my code does not work anymore.

Platform and pynput version MacOS Catalina 10.15.7 pynput version unknown

To Reproduce So I took the example code and flipped it around:

def darwin_intercept(event_type, event):
    import Quartz
    length, chars = Quartz.CGEventKeyboardGetUnicodeString(
        event, 100, None, None)
    if length > 0 and chars == 'x':
        # Suppress x
        return None
    elif length > 0 and chars == 'a':
        # Transform a to b
        Quartz.CGEventKeyboardSetUnicodeString(event, 1, 'b')
    else:
        return event
def darwin_intercept(event_type, event):
    import Quartz
    length, chars = Quartz.CGEventKeyboardGetUnicodeString(
        event, 100, None, None)
    if length > 0 and chars == 'a':
        # Transform a to b
        Quartz.CGEventKeyboardSetUnicodeString(event, 1, 'b')
    elif length > 0 and chars == 'c':
        # Transform c to d
        Quartz.CGEventKeyboardSetUnicodeString(event, 1, 'd')
    if length > 0 and chars == 'x':
        # Suppress x
        return None
    else:
        return event

Should be trivial, right? But now if I delete

    if length > 0 and chars == 'x':
        # Suppress x
        return None

my code does not work anymore. When I leave it in but replace if by elif, it does not work either. This is puzzling me...

SpecialCharacter commented 2 years ago

My bad. After some more searching, I found the mistake in one of my conditions.