我试着用这个上下文制作一个键盘句柄:
当f1按下“开始”(按下并释放此键: the )和按下f2时,将状态设置为false以停止按下键( the ):
from pynput.keyboard import Key, Listener
STATUS = True
def on_release(key):
if key == Key.f1:
# Stop listener
STATUS = False
if key == key.f2:
STATUS = True
def main():
with Listener(
on_release=on_release) as listener:
listener.join()
while STATUS:
keyboard.press('q')
keyboard.release('q')
keyboard.press('w')
keyboard.release('w')
keyboard.press('w')
keyboard.release('w')当我运行时,我得到了这个错误:
raceback (most recent call last):
File "ap.py", line 1, in <module>
from pynput.keyboard import Key, Listener
File "/usr/local/lib/python3.8/dist-packages/pynput/__init__.py", line 40, in <module>
from . import keyboard
File "/usr/local/lib/python3.8/dist-packages/pynput/keyboard/__init__.py", line 31, in <module>
backend = backend(__name__)
File "/usr/local/lib/python3.8/dist-packages/pynput/_util/__init__.py", line 76, in backend
raise ImportError('this platform is not supported: {}'.format(
ImportError: this platform is not supported: ('failed to acquire X connection: Bad display name ""', DisplayNameError(''))
Try one of the following resolutions:
* Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly编辑
我已经使用了pip3安装pynput
发布于 2022-11-02 22:53:25
在运行脚本之前,尝试使用以下命令连接到X服务器
xhost +例如,创建像这样的bash脚本(script.bash)
DISPLAY=:0
xhost +
python3 yourscript.py跑动
bash script.bashhttps://stackoverflow.com/questions/68785806
复制相似问题