TypeError: объект InputDevice не повторяется - PullRequest
0 голосов
/ 12 марта 2019

Я собираюсь вставить всю свою функцию здесь перед вопросом.

def trackBoth():
    #if event determination variable is 1 it has both K and M if it is 2 it is only K or M if 3 then none
    x_max = 800 #defines largest position mouse can go (19th LED)
    x_min = -800 #defines smallest position mouse can go (9th LED)
    y_max = 800
    y_min = -800
    try:
            keyboardEvent = evdev.InputDevice('/dev/input/event0') #setting keyboard to USB port
            mouseEvent = evdev.InputDevice('/dev/input/event1') #setting mouse to USB port
            event_det = 1
    except:
            try:
                    keyboardEvent = evdev.InputDevice('/dev/input/event0')
                    event_det = 2
            except:
                    try:
                            keyboardEvent = evdev.InputDevice('/dev/input/event1')
                            event_det = 2
                    except:
                            event_det = 3
                            NoKeyboardMouse()

    async def print_events(device):
            global x, y #must be inside of loop or else nonlocal error is thrown
            x = 0 #defines the start of x
            y = 0 #defines the start of y
            async for event in device.async_read_loop():
                    if event.type == ecodes.EV_REL: #if the event is a mouse moving
                            if event.code == ecodes.REL_X: #chechking for x direction
                                    print("REL_X", event.value)
                                    x += event.value #advance the cursor position
                                    #every 80 increments advance 1 led
                            if event.code == ecodes.REL_Y: #checking for y direction
                                    print("REL_Y", event.value)
                                    y += event.value * -1 #advance the cursor position
                            print(x, y)
                    if event.type == ecodes.EV_KEY: #if the event is a button click
                            c = categorize(event)
                            if c.keystate == c.key_down: #only reading key for down press not up
                                    print(c.keycode)
                                    if "BTN_LEFT" in c.keycode: #if it is a left mouse click go to check where the cursor is
                                            checkPosition()
                                    if c.keycode == "BTN_RIGHT": #if it is a right click override the current display
                                            rightClick()
                                    if c.keycode == "KEY_1":
                                            ser.write(str.encode('1'))
                                    if c.keycode == "KEY_2":
                                            ser.write(str.encode('2'))
                                    if c.keycode == "KEY_3":
                                            ser.write(str.encode('3'))
                                    if c.keycode == "KEY_4":
                                            ser.write(str.encode('4'))
                                    if c.keycode == "KEY_5":
                                            ser.write(str.encode('5'))
                                    if c.keycode == "KEY_6":
                                            ser.write(str.encode('6'))
                                    if c.keycode == "KEY_7":
                                            ser.write(str.encode('7'))
                                    if c.keycode == "KEY_8":
                                            ser.write(str.encode('8'))
                                    if c.keycode == "KEY_9":
                                            ser.write(str.encode('9'))
                                    if c.keycode == "KEY_0":
                                            ser.write(str.encode('0'))
                                    if c.keycode == "KEY_S":
                                            ser.write(str.encode('s'))
                                    if c.keycode == "KEY_G":
                                            ser.write(str.encode('g'))

    if (event_det == 1):
            for device in keyboardEvent, mouseEvent:
                    asyncio.ensure_future(print_events(device))
    if (event_det == 2):
            for device in keyboardEvent:
                    asyncio.ensure_future(print_events(device))

    loop = asyncio.get_event_loop()
    loop.run_forever()

В основном моя проблема заключается в этом разделе

    if (event_det == 1):
            for device in keyboardEvent, mouseEvent:
                    asyncio.ensure_future(print_events(device))
    if (event_det == 2):
            for device in keyboardEvent:
                    asyncio.ensure_future(print_events(device))

, когда я пытаюсь запустить этот код, предполагая, что event_detравный 2, я получаю эту ошибку

Traceback (most recent call last):
  File "etho.py", line 569, in <module>
    trackBoth()
  File "etho.py", line 559, in trackBoth
    for device in keyboardEvent:
TypeError: 'InputDevice' object is not iterable

при условии, что к ней подключены клавиатура и мышь, и она будет работать нормально (event_det = 1).Почему он выдает ошибку, когда пытается выполнить только одно событие (keyboardEvent), как мне изменить свой код здесь.

...