Raspberry PI GPIO 键盘/鼠标输入

问题描述

尝试使用 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 按钮扩展代码。有人知道吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)