简体   繁体   中英

Exiting python program using evdev InputDevice results in a error

I'm experimenting with a controller as an input device using evdev. When I exit the program, I get an error message stating that the delete method (super) requires at least one argument. I have looked, but wer not able to find a solution to handle this properly.

The program:

# 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)

When I use a specific key I break out the loop, resulting in this error message:

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

The same happens when I exit using ^C . Any ideas how to handle the exit properly?

Simply, evdev is waiting for an event to occur while suddenly you interrupt the cycle.

In order to exit from the execution properly, remove the break statement, close the device and end your script.

[...]
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()            
[...]

For exiting sharply with ^C without having errors, you still need to close the evdev input device, using a "try-except block".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM