下面带有pyudev
的用于打印所有连接的USB鼠标的Python 3代码显示3行,尽管仅连接了1个鼠标。
输出:
Device('/sys/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/0003:062A:4102.0001/input/input12')
Device('/sys/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/0003:062A:4102.0001/input/input12/event6')
Device('/sys/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/0003:062A:4102.0001/input/input12/mouse0')
为什么1个鼠标设备有3条线,如何固定?
代码:
#!/usr/bin/python3
import pyudev
class MouseMonitor:
def __init__(self):
self._context = pyudev.Context()
self._monitor = pyudev.Monitor.from_netlink(self._context)
self._monitor.start()
devices = self._context.list_devices(subsystem="input", ID_INPUT_MOUSE=True)
for device in devices:
print(device)
if __name__ == "__main__":
mouse_monitor = MouseMonitor()