'Unable to run 'keyboard.is_pressed' on Mac

Im trying to make a script where every time I press x, it prints y.

When I run the code:

import keyboard

if keyboard.is_pressed('x'):
    print ("y")

The console outputs:

   raise OSError("Error 13 - Must be run as administrator")
OSError: Error 13 - Must be run as administrator

Thanks!



Solution 1:[1]

You can't run this like you normally would in the mac terminal due to a security feature in macOS.

Let's assume your file name is script.py.

If you type

python3 script.py

in the terminal, it will pop up with the OSError because macOS sees this as a security breach.


You need to run this file as administrator.

In order to do so, you need to type

sudo python3 script.py

It will then ask you for your password as verification. After that, your script should work as expected.

Solution 2:[2]

The keyboard module registers global key events (they trigger without application focus) and this requires administrator permissions under MacOS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 d19mc
Solution 2 xdertz