[英]Can't use keyboard in raspberry pi
I'm using a raspberry pi 3A+ and have been trying to use the keyboard for hours.我正在使用 raspberry pi 3A+ 并尝试使用键盘几个小时。 I have written this code:我写了这段代码:
import keyboard
while True:
if keyboard.is_pressed("a"):
print("You pressed 'a'.")
break
Every time I run it the console just says this long error message:每次我运行它时,控制台都会显示这条长长的错误消息:
Traceback (most recent call last):
File "/home/yesnt/Documents/programming/NICK.py", line 3, in <module>
if keyboard.is_pressed("a"):
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/__init__.py", line 410, in is_pressed
_listener.start_if_necessary()
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/home/yesnt/.local/lib/python3.9/site-packages/keyboard/_nixcommon.py", line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.
I have only started playing with the raspberry today, so I still don't know a lot of things.今天才开始玩树莓派,很多东西还是不懂。 Can anyone help me fix this?谁能帮我解决这个问题? Also I'm trying to use it for a circuit to turn on and off a LED, but I think it won't have any interference on that.此外,我正在尝试将它用于打开和关闭 LED 的电路,但我认为它不会对此产生任何干扰。 Thanks谢谢
I've installed the keyboard in every single way shape and form, but nothing.我已经安装了各种形状和形式的键盘,但没有。 install keyboard, pip install keyboard, pip3 install keyboard, sudo pip3 install keyboard,....安装键盘,pip 安装键盘,pip3 安装键盘,sudo pip3 安装键盘,....
You do not have permission to access raw devices in /dev/input/input*
, that is documented in the package you use: https://pypi.org/project/keyboard/您无权访问/dev/input/input*
中的原始设备,这记录在您使用的 package 中: https://pypi.org/project/keyboard/
You could run your program as root (sudo myProgram.py) or check the permission of the input devices this script want to access.您可以以 root 身份运行您的程序 (sudo myProgram.py) 或检查此脚本想要访问的输入设备的权限。 I checked my linux machine and the devices are owned by root
and group input
, so it could be a solution to add the group input
to your user.我检查了我的 linux 机器,这些设备由root
和 group input
所有,因此将 group input
添加到您的用户可能是一个解决方案。 But you have to check the permission on YOUR raspberry pi.但是你必须检查你的树莓派的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.