我有一个小的 Python 脚本,可以将我的 Logitech G710 G 键从默认的 1-6 重新映射到 F13-18,手动调用时可以正常工作,例如:
./script.py /dev/hidraw3
我遇到的问题是设备插件上的脚本自动运行。目前我有一个udev从理论上来说,该规则应该有效,但事实却并非如此。
SUBSYSTEM=="hidraw", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c24d", ATTRS{bInterfaceNumber}=="01", ACTION=="add", RUN+="/usr/local/bin/g710.py %k"
整个脚本:
#!/usr/bin/python3
import sys
import hid
def bindkeys(node):
board = hid.device()
board.open_path(sys.argv[1].encode())
board.send_feature_report([9, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0, 0, 0, 0, 0, 0])
print("script fired")
print(board.get_manufacturer_string())
board.close()
def main(args):
bindkeys(args)
if __name__ == '__main__':
main(sys.argv[1].encode())
怎么了?
答案1
好吧,似乎距离SUBSYSTEM=="hidraw"
太远而SUBSYSTEM=="usb"
无法使用ATTRS{bInterfaceNumber}
,所以,我只是重新调整了脚本以找到正确的 hidraw 节点本身,并且仅在插入正确的设备时触发它:
#!/usr/bin/python3
import hid
def bindkeys(node):
board = hid.device()
board.open_path(node)
board.send_feature_report([9, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0, 0, 0, 0, 0, 0])
board.close()
def find_g710():
for device in hid.enumerate():
if device['vendor_id'] == 0x046d and device['product_id'] == 0xc24d and device['interface_number'] == 1:
return device['path']
def main():
g710 = find_g710()
bindkeys(g710)
if __name__ == '__main__':
main()
使用以下 udev 规则:
SUBSYSTEM=="hidraw", ACTION=="add", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c24d", RUN+="/usr/local/bin/g710.py"