从函数中获取多个变量值或信息的最佳方法

问题描述

我对 python 还很陌生,正在尝试创建一个自定义模块,我可以使用 evdev(不是那么重要)为我的 raspberry pi 导入该模块。我的问题是我需要一种很好的方法来知道何时按下给定按钮,最好使用 True/False。如果我要导入此模块并使用 read_stream() 函数,那么能够收集多种类型的输入的最简单方法是什么。 (只是为了澄清,gamepad.read_loop() 每次迭代只返回一个值。

from evdev import InputDevice,categorize,ecodes

aBtn = 304
bBtn = 305
xBtn = 307
yBtn = 308

LBmper = 310
RBmper = 311

MenuBtn = 315
WindowBtn = 314
XboxBtn = 316

HorizontalDp = 16
VerticalDp = 17

RightT = 5
LeftT = 2

LeftStickClick = 317
RightStickClick = 318

Ls_X = 0
Ls_Y = 1
Rs_X = 3
Rs_Y = 4

def read_stream(input_path,trigger_deadzone):
    '''Begins the loop that reads all of the incoming events.'''
    button_list = ['aBtn_pressed','bBtn_pressed','xBtn_pressed','yBtn_pressed','lBmper_pressed','rBmper_pressed','mBtn_pressed','wBtn_pressed','xbxBtn_pressed','rDp_pressed','lDp_pressed','dDp_pressed','uDp_pressed','Ls_clicked','Rs_clicked','Ls_right','Ls_left','Ls_down','Ls_up','Rs_right','Rs_left','Rs_down','Rs_up','Lt_custom','Rt_custom']
    for button in button_list:
        button = False
    gamepad = InputDevice(input_path)
    if (not "Microsoft X-Box One S pad" in str(gamepad)):
        raise Exception("Invalid input device,please make sure you are using an Xbox One S controller.\nAlso make sure that the correct input path is set.")
    print("Stream is reading: ",gamepad)
    for event in gamepad.read_loop():
        value = event.value
        code = event.code
        if (code == aBtn):
        #if (304 <= code <= 308):
            if (code == aBtn):
                if (value == 1):
                    aBtn_pressed = True
                else:
                    aBtn_pressed = False
            elif (code == bBtn):
                if (value == 1):
                    bBtn_pressed = True
                else:
                    bBtn_pressed = False
            elif (code == xBtn):
                if (value == 1):
                    xBtn_pressed = True
                else:
                    xBtn_pressed = False
            else:
                if (value == 1):
                    yBtn_pressed = True
                else:
                    yBtn_pressed = False
        else:
            print("Unfiltered stream input.")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)