Looking for a string in device capabilities; evdev

2019-08-27 09:07发布

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!

标签: python evdev
1条回答
smile是对你的礼貌
2楼-- · 2019-08-27 09:50

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.

查看更多
登录 后发表回答