简体   繁体   English

无法在 Mac 上运行“keyboard.is_pressed”

[英]Unable to run 'keyboard.is_pressed' on Mac

Im trying to make a script where every time I press x, it prints y.我试图制作一个脚本,每次我按 x 时,它都会打印 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!谢谢!

You can't run this like you normally would in the mac terminal due to a security feature in macOS.由于 macOS 中的安全功能,您无法像通常在 mac 终端中那样运行它。

Let's assume your file name is script.py.假设您的文件名为 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.在终端中,它会弹出 OSError,因为 macOS 将其视为安全漏洞。


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.之后,您的脚本应该按预期工作。

键盘模块注册全局按键事件(它们在没有应用程序焦点的情况下触发),这需要 MacOS 下的管理员权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM