[英]python3 linux - detect keyboard key pressed withput root
我试图检测键盘按键但没有root - 我找到了库键盘,但它没用(因为它需要root)
我发现一些网站说它不需要root,但它绝对需要。
我试过这段代码
import keyboard
def key_press(key):
print(key.name)
keyboard.on_press(key_press)
但就像我说的 - 它需要root
...
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.
我需要没有root,因为当然是第一个安全性,因为我稍后会添加pygame - 你不能用root运行gui
我也尝试搜索其他库,但是我没有找到任何用于检测按键的内容 - 有按键的pykeyboard,但没有检查按键是否被按下
在本模块的已知限制下提到了它
为了避免依赖X,Linux部分读取原始设备文件(
/dev/input/input*
),但这需要root。
可以从源代码( _nixkeyboard.py )确认。
def ensure_root():
if os.geteuid() != 0:
raise ImportError('You must be root to use this library on linux.')
device = None
def build_device():
global device
if device: return
ensure_root()
device = aggregate_devices('kbd')
def init():
build_device()
...
def listen(callback):
build_device()
...
def write_event(scan_code, is_down):
build_device()
...
请注意,在做任何动作之前build_device
被调用时,它调用ensure_root
检查调用进程的有效用户ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.