我有一個小的 Python 腳本,可以將我的 Logitech G710 G 鍵從預設的 1-6 重新映射到 F13-18,在手動呼叫時工作正常,例如:
./script.py /dev/hidraw3
我遇到的問題是在設備插件上自動運行腳本。目前我有一個烏德夫理論上應該有效的規則,但事實並非如此。
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"