我要在问题之前在此处粘贴我的整个函数。
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)时,为什么会给我错误?我应该在这里如何更改代码。