1

我试图将其提炼为要点。这段代码工作正常。

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)

但是,如果我将操纵杆函数移动到单独的文件(包括正确的 evdev 导入)并导入它,代码会中断(函数“操纵杆”将始终返回 false,无法识别操纵杆事件)。

任何想法为什么?我知道软件/硬件接口可能很棘手,但这很荒谬。

4

1 回答 1

0

发现了错误。当我将joystick函数移到文件之外时,我忽略了用它移植button字典。该try声明掩盖了我通常会通过省略字典而得到的错误。

于 2020-11-14T20:56:09.260 回答