Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stepheneb opened this issue Sep 23, 2023 · 1 comment
Open

Comments

@stepheneb
Copy link

stepheneb commented Sep 23, 2023

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants