我想提供一个/dev/DEVICE路径作为输入,并将设备“人性化”名称作为输出。
我成功地从ID_MODEL_ENC获得了这个名字,就像在下面这个片段中:
def dev_name(dev_path):
from pyudev import Context
for device in Context().list_devices(DEVNAME=dev_path):
print device.get('ID_MODEL_ENC').decode('string_escape')但它不适用于蓝牙设备。ID_MODEL_ENC似乎没有得到如此广泛的应用。
在我的应用程序中,我只将它用于操纵杆,然后设备路径将始终是/dev/input/js*。
示例1: USB操纵杆是js0
$ dev_name.py /dev/input/js0
Twin USB Joystick示例2:蓝牙操纵杆是js2
$ dev_name.py /dev/input/js2
Traceback (most recent call last):
File "pyudev_get_js_name.py", line 9, in <module>
dev_name(sys.argv[1])
File "pyudev_get_js_name.py", line 7, in dev_name
print '"'+ device.get('ID_MODEL_ENC').decode('string_escape') +'"'
AttributeError: 'NoneType' object has no attribute 'decode'这显然是因为该设备没有ID_MODEL_ENC属性。
为了确保系统知道设备的名称,我们可以直接在shell提示符中这样做:
$ sys_dev_path="$(udevadm info --name=/dev/input/js2 | grep DEVPATH | cut -d= -f2)"
$ cat "/sys$(dirname $sys_dev_path)/name"
8Bitdo NES30 GamePad我知道我可以使用python制作类似的内容,并检查/sys/devices/.../name文件的内容,但它看起来像是临时的。有没有办法让pyudev给我取个操纵杆的名字?
备注:我知道用玩偶游戏来获得游戏游戏的游戏名很简单,但是这里不是一个选项。
提前谢谢。
发布于 2017-05-01 04:58:22
我用控制器检查了pyudev创建的设备对象,对象的任何成员都没有保存描述性名称。据我所知,它在文档中也没有提到。不管怎样,我就是这样实现名字功能的。就像你说的,我只是从名字文件中提取出来。
def get_devices():
context = Context()
##devices = []
#equivalent to "ls /dev/input | grep js"
js_list = [d for d in os.listdir("/dev/input") if d.startswith("js")]
for js in js_list:
js_file = os.path.join("/dev/input", js)
#different syntax to only get one item from Context()
#.sys_path gives only the full system path
js_path = Devices.from_device_file(context, js_file).sys_path
#the name file should exist at this combined path
name_path = os.path.join(js_path, "device", "name")
#read the name from that file
with open(name_path, "r") as buf:
js_name = buf.read().strip()
print("File: {}".format(js_path))
print("Name: {}".format(js_name))
##devices.append(...)
##return devices发布于 2017-05-01 08:44:38
如果pyudev没有给出您所需的内容,请尝试evdev。
>>> from evdev import InputDevice,list_devices
>>> devices = [InputDevice(fn) for fn in list_devices()]
>>> for dev in devices:
... print (dev.fn, dev.name)
...
/dev/input/event12 HDA Intel PCH HDMI/DP,pcm=3
/dev/input/event11 HDA Intel PCH Front Headphone
/dev/input/event10 HDA Intel PCH Line Out
/dev/input/event9 HDA Intel PCH Rear Mic
/dev/input/event8 HDA Intel PCH Front Mic
/dev/input/event7 RDing FootSwitch3F1.
/dev/input/event6 USB OPTICAL MOUSE
/dev/input/event5 BTC USB Multimedia Keyboard
/dev/input/event4 BTC USB Multimedia Keyboard
/dev/input/event3 Video Bus
/dev/input/event2 Power Button
/dev/input/event1 Sleep Button
/dev/input/event0 Power Buttonhttp://python-evdev.readthedocs.io/en/latest/
对于你对操纵杆的评论,简单的回答不是直接的,而是如果你能找到所有操纵杆都有的功能,但没有其他的功能,那就可以用来过滤掉所有的非操纵杆设备。我有一个类似的要求,我只需要识别脚踏板装置。为了达到这个目的,我创建了一个文件,其中包含了所有已知的脚踏板的USB和Procuct的列表。在此基础上,我只检查了每个设备的dev.info.vendor和dev.info.product项,以确定是否匹配。为了允许使用未知设备的人,如果我没有找到匹配的设备,我展示了所有的设备,并要求他们识别他们的脚踏板(操纵杆),我只需将其附加到列表中,并附加到/lib/udev/ file es.d/51-足切换.规则文件。下面是检查代码:
devices = [InputDevice(fn) for fn in list_devices()]
for dev in devices:
vendor = "%x" % dev.info.vendor
vendor = vendor.zfill(4)
product = "%x" % dev.info.product
product = product.zfill(4)
check_pedal_string = vendor+":"+product
event_id = dev.fn
if check_pedal_string in known_devices:
found_hid = event_id
breakhttps://stackoverflow.com/questions/43599936
复制相似问题