4

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

5 Answers5

10

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.

pookie
  • 3,796
  • 6
  • 49
  • 105
  • 2
    For me, it was enough to just add Terminal and the IDE (Visual Studio Code) to the list. I could not add Python or the folder where it is installed (as stated in step 4), and apparently it’s not needed. – Gino Mempin Jan 11 '20 at 00:16
  • 1
    On Mojave 10.14.6 I'm also unable to select my Python installation in step 4. Running the app as root also does not work for the native terminal app (only in 3rd party terminal emulators). – Reticulated Spline Feb 06 '20 at 05:00
3

Found the problem.

For some strange reasons; OSX is uber-picky about returning events, so unless you go in the security settings and enable Pycharm to be in the list of apps that are allowed to use accessibility, it won't work.

I didn't try on Windows yet, but I assume it will be the same issue. The only gripe I have is that I have no idea how to add Python itself to the list of supported accessibility apps; since the control panel does not allow me to go in /usr/local/bin, which is where I have Python3 installed (via Brew).

1

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

  • Preference
  • Security & Privacy
  • Input Monitoring -> confirm PyCharm
deus mouse
  • 11
  • 1
0

Some people have stated that adding IDLE to supported accessibility apps is what allows python itself to run the listener.

while your in a finder window, if you press cmd+shft+'.' (period key) it will show hidden files, which will allow you to navigate to usr/local/bin and look for your python implementation.

On windows this is slightly different, I always run python/pycharm as admin and it never gives me any issues.

5nizzard
  • 43
  • 7
0

Try superuser ($sudo su) and run your python code in terminal, I think it should work

Im was working with OSX 10.12 and pynput was only getting cmd ctrl fn and option keys when pressed but now in superuser it gets the keys.

Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
Santiago
  • 11
  • 2