3

Can't seem to find any solution to the problem. When I try to run the following code the console response with "This process is not trusted! Input event monitoring will not be possible until it is added to accessibility clients."

I am using PyCham on a MacOS Big Sur device. My Mac is not asking for any permissions. I ran exact same code on a Windows machine without any problems. Can't find any help online.

What am I missing??

import pyautogui
from pynput import keyboard


def on_activate_h():
    print(pyautogui.position())


with keyboard.GlobalHotKeys({
    "<ctrl>+<alt>+r": on_activate_h}) as h:
    h.join().   
Mathias
  • 31
  • 1
  • 2
  • I think the same problem is on Linux. All for security reason. It may need some changes in system. It may work if you run it with `sudo` (`SuperUserDO`) to have admin/root privilages - `sudo python script.py`. So all this is not problem with PyCharm or Python code but only with system settings which want to make your system secure. – furas Oct 18 '21 at 23:15

3 Answers3

2

I am not sure why PyCharm isn't working correctly. I am using Visual Studio Code and I had the same problem as you.

A. Terminal.app (temporary solution)

If we run it in the Terminal.app and give that permissions, then it should work for you like it did for me.

  1. Settings -> Security & Privacy
  2. Click on the Privacy tab
  3. Scroll and click on the Accessibility Row
  4. Click the +
  5. Navigate to /System/Applications/Utilities/ or wherever the Terminal.app is installed
  6. Click okay.

Alternate solution here

B. py2app (distributable app)

Another approach is to use py2app to make a *.app and give that permissions.

  1. Make a setup.py
from setuptools import setup

APP = ['main.py'] # points to your main python file
DATA_FILES = []
OPTIONS = {
    'packages': ['pynput'] # include your other dependencies here
}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)
  1. In the terminal, run pip install py2app
  2. And then, python setup.py py2app
    • This will make a folder named dist.
  3. Add ./dist/main.app to the Accessibility tab using the same steps as the first solution.
  4. Just to be sure, add main (in the folder ./dist/main.app/Contents/MacOS/) to the Accessibility tab. You can type the directory in the window by pressing G
  5. Either open the app in Finder or run it in the console.
IcyIcicle
  • 564
  • 3
  • 15
1

You may look at Mac Setting - Privacy - Input Listener then allow PyCharm.app to listen input from keyboard√

  • If using VSCode, `System Settings` -> `Privacy & Security` -> `Input Monitoring` -> allow `Visual Studio Code` – WhaSukGO Mar 22 '23 at 14:36
0

I run on vs code terminal and below step for allow listen event:

  1. Open System Setting > Privacy & Security
  2. Open Input Monitoring
  3. Press '+' button and add vscode-app to the list

enter image description here

nguyên
  • 5,156
  • 5
  • 43
  • 45