我试图将其提炼为要点。这段代码工作正常。
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,无法识别操纵杆事件)。
任何想法为什么?我知道软件/硬件接口可能很棘手,但这很荒谬。