使用Python Evdev的多个游戏板?

2024-05-08 13:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我找到了一种在Python中使用gamepad的方法,使用了Evdev模块(参见末尾的链接)。 在本教程中,作者只使用了一个gamepad,但他也声明,应该可以使用基于以下代码的多个gamespad:

from evdev import InputDevice
from select import select
gamepad = InputDevice('/dev/input/event0')
while True:
    r,w,x = select([gamepad], [], [])
    for event in gamepad.read():
        print(event)

在选择。选择似乎要等到一个按钮被按下,所以程序被中断,直到那发生。如何修改代码以使用多个游戏板或在等待按钮输入时执行其他代码? 或者有没有更好的替代方法来使用evdev呢?在

http://ericgoebelbecker.com/2015/06/raspberry-pi-and-gamepad-programming-part-1-reading-the-device/


Tags: 模块方法代码fromimportevent链接教程
1条回答
网友
1楼 · 发布于 2024-05-08 13:35:38

How can I modify the code to use multiple gamepads or to execute other code while waiting for button-inputs?

查看^{}的文档

read()
Read multiple input events from device. Return a generator object that yields InputEvent instances. Raises BlockingIOError if there are no available events at the moment.

选择将阻止,直到输入事件可用。相反,我们可以读取事件,直到得到BlockingIOError。然后继续下一个游戏板,或者做任何其他需要在主循环中完成的工作。在

您也可以考虑使用^{}

read_one()
Read and return a single input event as an instance of InputEvent.

Return None if there are no pending input events.

相关问题 更多 >