1

在 evdev 中,我试图检查是否插入了鼠标和键盘,如果有,则将设备路径分配给要使用的变量。这工作了一段时间,因为我刚刚使用此代码检查了设备名称中的鼠标或键盘名称

if ("KEYBOARD" in device.name) or ("Keyboard" in device.name):
                    print ("This is a Keyboard")
                    keyboarddir = device.path
                    keyboard = evdev.InputDevice(keyboarddir)

插入不同的鼠标后,我发现他们并没有都说鼠标在里面,我想知道是否有一种方法可以将名为“BTN_RIGHT”的字符串与设备功能进行比较。我输入的不起作用的代码会是这样的。

if ("BTN_RIGHT" in device.capabilities(verbose=True)):
                    print ("this is the mouse")

请帮助我弄清楚如何更容易地检测鼠标,或者实际上能够搜索它的功能并将它们与其他字符串进行比较!

4

1 回答 1

0

Since the data structure you want to parse looks like:

{ 1: [272, 273], 3: [0, 1] }

...you might do something like (not using verbose=True here, since it's a lot simpler if we're just working with the raw constants):

caps = device.capabilities()
has_rmb = evdev.ecodes.BTN_RIGHT in caps.get(evdev.ecodes.EV_KEY, [])

If you really want to work with the string forms (which I don't recommend), your data would instead look like:

{ ('EV_KEY', 1): [('BTN_MOUSE', 272), ('BTN_RIGHT', 273), ...],
  ('EV_ABS', 3): [(('ABS_X', 0), AbsInfo(min=0, max=15360, fuzz=128, flat=0)),
                  (('ABS_Y', 1), AbsInfo(min=0, max=10240, fuzz=128, flat=0)),] }

...you might do something like:

caps = device.capabilities()
key_codes = evdev.ecodes[('EV_KEY', ecodes.EV_KEY)]
has_rmb = 'BTN_RIGHT' in [ kc[0][0] for key_codes ]

...but that's a lot of extra code and overhead to work around cruft that's only in the data structures for purposes of human readability.

于 2019-02-20T20:37:44.093 回答