Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pynput keyboard listener does not detect keys on Mac OS X

I am using pynput to record keystrokes via Listener on OS X Mojave. I am using Pycharm as my IDE for running the code.

I was not able to get it to work using the same example from the pynput site.

from pynput.keyboard import Listener as key_listener

class recorder:

    def on_press(self, key):
        print(key)

    def on_release(self, key):
        print(key)


if __name__ == "__main__":
    testme = recorder()

    with key_listener(on_press=testme.on_press, on_release=testme.on_release) as listener:
        listener.join()

I did step through it and I get no errors (unless I put the with statement in a function, instead of in the main, but that's a known issue with threading in Mojave, from what I can tell after searching for that error), but everything stops at the .join() statement, and I get nothing printed when I press and release a key on my keyboard.


1 Answers

This is probably a bit late, but the answer is to go into:

  1. Settings -> Security & Privacy
  2. Click on the Privacy tab
  3. Click the + Hold down CMD + SHIFT + . (so that you can see hidden files/folders)
  4. Navigate to /usr/local/bin or wherever you have Python installed
  5. Click okay.

That should do it.

Note

If you try to run your app via the terminal, you will need to add the terminal.app to the list of allowed apps, as done above for Python.

like image 131
pookie Avatar answered Sep 18 '25 08:09

pookie