尝试使用 Raspberry Pi 的 GPIO 引脚创建非常简单的键盘和鼠标控制器。很想在教育环境中使用该代码,因此尝试为儿童/学生构建超级简单、易读的代码。但是代码有点奇怪,它一直输出相同的击键:
import gpiozero
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController
keyboard = KeyboardController()
mouse = MouseController()
Up = gpiozero.Button(26, bounce_time=0.02)
LeftMouse = gpiozero.Button(17, bounce_time=0.02)
while True:
if Up.is_pressed:
print("Up")
keyboard.press(Key.up)
Up.wait_for_release()
keyboard.release(Key.up)
elif LeftMouse.is_pressed:
print("Left Mouse button")
mouse.press(button.left)
LeftMouse.wait_for_release()
mouse.release(button.left)
使用 Python 3.7.3。无论我触发哪个 GPIO(26 或 17),代码始终输出“向上”并按下键盘“向上”按钮。这一定是愚蠢的,但我似乎无法弄清楚。很想继续使用 if/elif,所以我以后可以用更多的 GPIO 按钮来扩展代码。有什么想法吗?