【问题标题】:Exiting python program using evdev InputDevice results in a error使用 evdev InputDevice 退出 python 程序会导致错误
【发布时间】:2020-07-12 23:57:04
【问题描述】:

我正在尝试使用 evdev 将控制器作为输入设备。当我退出程序时,我收到一条错误消息,指出删除方法(超级)至少需要一个参数。我看过,但无法找到正确处理此问题的解决方案。

程序:

# import evdev
from evdev import InputDevice, categorize, ecodes

# creates object 'gamepad' to store the data
# you can call it whatever you like
gamepad = InputDevice('/dev/input/event5')

# prints out device info at start
print(gamepad)

# evdev takes care of polling the controller in a loop
for event in gamepad.read_loop():
    # filters by event type
    if event.type == ecodes.EV_KEY and event.code == 308:
        break
    if event.type == ecodes.EV_ABS and event.code == 1:
        print(event.code, event.value)
    if event.type == ecodes.EV_ABS and event.code == 0:
        print(event.code, event.value)
    # print(categorize(event))
    if event.type == ecodes.EV_KEY:
        print(event.code, event.value)

当我使用特定键时,我会中断循环,导致以下错误消息:

Exception TypeError: TypeError('super() takes at least 1 argument (0 given)',) in <bound method InputDevice.__del__ of InputDevice('/dev/input/event5')> ignored

当我使用^C 退出时也会发生同样的情况。 任何想法如何正确处理退出?

【问题讨论】:

    标签: python-3.x controller evdev


    【解决方案1】:

    简单地说,evdev 正在等待一个事件发生,而你突然中断了循环。

    为了正确退出执行,请删除 break 语句,关闭设备并结束脚本。

    [...]
    for event in gamepad.read_loop():
        if event.type == ecodes.EV_KEY and event.code == 308:
            gamepad.close()   #method of ..yourpythonlib/evdev/device.py
            quit()            
    [...]
    

    要使用^C 快速退出而不会出现错误,您仍然需要使用“try-except 块”关闭 evdev 输入设备。

    for event in gamepad.read_loop():
       try:
           [...] if event [...]
       except KeyboardInterrupt:
           gamepad.close()
           raise  
    

    【讨论】:

      猜你喜欢
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 2011-04-27
      • 2012-12-16
      • 2013-06-25
      相关资源
      最近更新 更多