moses-palmer / pynput

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

Context manager doesn't work with suppress param on macOS #468

Open mseifrid opened 2 years ago

mseifrid commented 2 years ago

Description suppress=True results in the Listener running through the code in the context manager, but won't give time for any inputs. Seems to work fine on Windows 10 and Ubuntu 20.04.

Platform and pynput version OS: macOS 11.6.5 Python: 3.9 pynput: 1.7.6

To Reproduce

def on_press(key):
    print(key)

def on_release(key):
    if key == keyboard.Key.esc:
        # Stop listener
        return False

with keyboard.Listener(on_press=on_press, on_release=on_release, suppress=True) as listener:
    print('suppress')
    listener.join()
print('context manager done')
hmijail commented 1 year ago

Just found this same issue on macOS Ventura 13.1, python 3.11, pynput 1.7.6.

Just modifying the HotKey example in the docs to add suppress = True causes it to fail silently.

Trying to add a darwin_intercept function as mentioned in the FAQ causes the same silent failure.

from pynput import keyboard

def on_activate():
    print('Global hotkey activated!')

def for_canonical(f):
    return lambda k: f(l.canonical(k))

hotkey = keyboard.HotKey(
    keyboard.HotKey.parse('<cmd>+h'),
    on_activate)
with keyboard.Listener(
        on_press=for_canonical(hotkey.press),
        on_release=for_canonical(hotkey.release), 
        suppress=True                   # <------ causes example to fail silently
        ) as l:
    l.join()