Looking for a string in device capabilities; evdev; python

扶醉桌前 提交于 2019-12-11 15:38:57

问题


in evdev i'm trying to check to see if there is a mouse and keyboard plugged in and if so assign the device path to a variable to be used. This worked for a while as i just checked for the name Mouse or Keyboard in the device name by using this code

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

After plugging in a different mouse i discovered that they don't all say mouse in there and i wanted to know if there was a way i could compare a string called "BTN_RIGHT" to the device capabilities. The code i typed which doesn't work would go something like this.

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

Please help me figure out how to detect a mouse easier or by actually being able to search through its capabilities and compare them to other strings!


回答1:


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.



来源:https://stackoverflow.com/questions/54794566/looking-for-a-string-in-device-capabilities-evdev-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!