我正在试验使用evdev将控制器作为输入设备。当我退出程序时,我收到一条错误消息,指出delete方法(super(至少需要一个参数。我已经看过了,但没能找到一个正确处理这个问题的解决方案。
程序:
# 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
退出时也会发生同样的情况。你知道如何正确处理出口吗?
简单地说,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
急剧退出,您仍然需要关闭evdev输入设备,使用";尝试除块";。
for event in gamepad.read_loop():
try:
[...] if event [...]
except KeyboardInterrupt:
gamepad.close()
raise