Unable to run 'keyboard.is_pressed' on Mac
Asked Answered
U

4

10

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!

Undervest answered 23/10, 2018 at 2:43 Comment(6)
Error message seems straightforward. Try running it as administrator. Maybe the keyboard module needs that in OSX.Shingles
Could you also show us the entire console output? I assume that line of code is only the end of the traceback, and you don't actually have a raise OSError in your code.Shingles
Yea, here is the entire error: Exception in thread Thread-1: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner self.run() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run self._target(*self._args, **self._kwargs)Undervest
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keyboard/__init__.py", line 292, in listen _os_keyboard.listen(self.direct_callback) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keyboard/_darwinkeyboard.py", line 430, in listen raise OSError("Error 13 - Must be run as administrator") OSError: Error 13 - Must be run as administratorUndervest
Im not sure how to run as administratorUndervest
Can you edit your question to put in the full traceback? I don't know much about OSX so I can't really help but I'm sure it'll be easier for someone else to answer your question that way.Shingles
V
6

You can't run a script with virtual keyboard inputs like you regular python file in the macOS terminal due to a security feature.

Let's assume your filename is script.py.

If you type

python3 script.py

macOS would view this as a security breach as recording keyboard inputs (like keyboard.is_pressed('x')) is a typical method for recording someone's password as they type it in on a website, application, etc.


To prevent that error, you'd need to run the file as an administrator.

To do so, type:

sudo python3 script.py

It will ask you for your user's password and then proceed to execute the code.

Vicarious answered 10/8, 2019 at 23:39 Comment(2)
This (sudo) resulted in "sh: segmentation fault sudo python3 pythonstuff.py" on macOS Monterey.Mayda
keyboard is a module for windows and linux (that's what it says while installing it)Lao
A
2

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

Assure answered 12/2, 2019 at 14:22 Comment(1)
Which makes this module pretty much useless ... who would like to run Python code as administrator (or any other code for that matter).Mayda
D
0

You could also go to system settings -> Privacy & Security and find give python access to control your keyboard

Dacy answered 17/11, 2023 at 9:13 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Neighborhood
R
0

You should start idle3 using sudo in terminal, and then in that sudo session of idle3 open your .py file which use keyboard module. It works fine on my Mac OS 10.15.7 and Python 3.12.3

sudo idle3
Regulation answered 21/5, 2024 at 19:15 Comment(1)
Note that running an IDE or any code or basically anything with super user permissions is a possible security risk. This might solve the question at hand but may introduce more serious problems.Gadgetry

© 2022 - 2025 — McMap. All rights reserved.