1

I've tried to distill this to the essentials. This code works fine.

from evdev import InputDevice, ecodes
dev = InputDevice('/dev/input/event2')   # this is the joystick event file
button = {304: 'A', 305: 'B', 307: 'X', 308: 'Y' }  # Xbox360 button mappings
exit = False

def joystick(dev):
    try:
        for event in list(dev.read()):
            if button[event.code] == 'A' and event.value == 1:
                print('Button A pressed.')
            if button[event.code] == 'X' and event.value == 1:
                print('Button X pressed.')
                return True
    except: pass
    return False
    
while exit == False:
    exit = joystick(dev)

However, if I move the joystick function to a separate file (including the proper evdev import) and import it the code breaks (function "joystick" will always return false, not recognizing joystick events).

Any ideas why? I know the software/hardware interface can be tricky but this is ridiculous.

user52326
  • 11
  • 2

1 Answers1

0

Found the bug. When I moved the joystick function outside of the file I neglected to port the button dictionary with it. The try statement masked the error I normally would have gotten by omitting the dictionary.

user52326
  • 11
  • 2