moses-palmer / pynput

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

macos 13.5.2, keyboard listener stopped working after upgrading xcode #574

Open stepheneb opened 1 year ago

stepheneb commented 1 year ago

Description This morning I was using keyboard.Listener in a small python program and everything worked. I upgraded to the latest xcode (Version 15.0 (15A240d) and let it finishing installing new frameworks for ios and macos development and key presses no longer get to the listener, instead they are echoed to the console.

I also restarted the computer after installing xcode -- so it's possible Apple applied one of it's invisible security updates. I have those enabled. Actual OS updates have to ask me.

Am very curious if this issue arises for anybody else.

Platform and pynput version Your operating system and version, and the version of pynput.

macos 13.5.2, pynput 1.7.6, python 3.11.5

To Reproduce If possible, please include a short standalone code sample that reproduces the bug. Remember to surround it with code block markers to maintain indentation!

Code sample extracted from larger program:

# https://pynput.readthedocs.io/en/latest/keyboard.html
from pynput import keyboard

running = True

def on_press(key):
    global running
    try:
        match key:
            case key.up:
                print("arrow-up")
            case key.right:
                print("arrow-right")
            case key.down:
                print("arrow-down")
            case key.left:
                print("arrow-left")
            case key.enter | key.esc:
                print("enter or esc")
                running = False
                return False
    except AttributeError:
        running = False
        return False

def on_release(key):
    pass

listener = keyboard.Listener(
    on_press=on_press, on_release=on_release)
listener.start()

print("\nPress arrows to adjust, enter or esc to quit\n")

def main():
    while running:
        pass

main()

This morning it worked fine.

Now pressing the left and right arrow keys and Enter results in output like this and I have to press ctrl-C to quit the program.

% python3 simple-keyboard.py

Press arrows to adjust, enter or esc to quit

^[[C^[[C^[[D^[[D

Steps that didn't work:

Added the following Python to Security and Preferences list of programs that have access to Accessibility features (this is the Python that is running when the simple-keyboard program is running):

/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python

Running with sudo:

% sudo python3 simple-keyboard.py
Password:

Press arrows to adjust, enter or esc to quit

^[[C^[[C^[[D^[[D

Removing suppress=True makes no difference in the results.

moses-palmer commented 1 year ago

Thank you for your report.

Could it be that the update included pyobjc? I unfortunately no longer have access to a macOS system, but I think some version of pyobjc is included with some Apple product. You can check this by importing Quartz in your script and checking its __file__ attribute. It should come from your virtualenv.