LINUX hwbd规则绑定(Mapping设备的event)

正文索引 [隐藏]

找到自己的设备

我以我这个项目用到的无限蓝牙手柄ACGM R1为例子

由于linux没有手柄驱动,读入手柄之后,四个轴识别为了音量加减和停止播放以及下一首歌曲。home和返回按键没有响应。
终端输入,

cat /proc/bus/input/dev

找到我们的设备,我们发现有两个输入和我们的设备有关

I: Bus=0005 Vendor=248a Product=8266 Version=3634
N: Name="ACGAM R1           Consumer Control"
P: Phys=08:71:90:43:F4:58
S: Sysfs=/devices/virtual/misc/uhid/0005:248A:8266.0005/input/input29
U: Uniq=FF:FF:96:01:69:27
H: Handlers=kbd event9 
B: PROP=0
B: EV=13
B: KEY=103800000000 e178000000000 0
B: MSC=10

I: Bus=0005 Vendor=248a Product=8266 Version=3634
N: Name="ACGAM R1          "
P: Phys=08:71:90:43:F4:58
S: Sysfs=/devices/virtual/misc/uhid/0005:248A:8266.0005/input/input30
U: Uniq=FF:FF:96:01:69:27
H: Handlers=event24 js0 
B: PROP=0
B: EV=1b
B: KEY=fff000000000000 0 0 178000000000 0
B: ABS=3
B: MSC=10

找到输入信息

以Consumer Control的event9 为例,终端输入

sudo evtest /dev/input/event9

向上拨动手柄发现输出(这里我已经配置手柄向上为方向上按键)

Input driver version is 1.0.1
Input device ID: bus 0x5 vendor 0x248a product 0x8266 version 0x3634
Input device name: "ACGAM R1           Consumer Control"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 103 (KEY_UP)
    Event code 104 (KEY_PAGEUP)
    Event code 105 (KEY_LEFT)
    Event code 106 (KEY_RIGHT)
    Event code 108 (KEY_DOWN)
    Event code 113 (KEY_MUTE)
    Event code 114 (KEY_VOLUMEDOWN)
    Event code 115 (KEY_VOLUMEUP)
    Event code 163 (KEY_NEXTSONG)
    Event code 164 (KEY_PLAYPAUSE)
    Event code 165 (KEY_PREVIOUSSONG)
    Event code 172 (KEY_HOMEPAGE)
  Event type 4 (EV_MSC)
    Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit)
Event: time 1580907977.954137, type 4 (EV_MSC), code 4 (MSC_SCAN), value c00e9
Event: time 1580907977.954137, type 1 (EV_KEY), code 103 (KEY_UP), value 1
Event: time 1580907977.954137, -------------- SYN_REPORT ------------
^[[AEvent: time 1580907978.039495, type 4 (EV_MSC), code 4 (MSC_SCAN), value c00e9
Event: time 1580907978.039495, type 1 (EV_KEY), code 103 (KEY_UP), value 0
Event: time 1580907978.039495, -------------- SYN_REPORT ------------

配置文件

/etc/udev/hwdb.d文件夹里 新建任意.hwdb文件 我们以ACGAM.hwdb为例
输入
evdev:input:b<bus_id>v<vendor_id>p<product_id>e<version_id>-<modalias>
其中input:b<bus_id>v<vendor_id>p<product_id>由第一步得到

换行输入(注意开头有空格)新的动作

KEYBOARD_KEY_<scan_code>=<key_code>

scan_code为第二步 (MSC_SCAN), value 的数值
查看keycode的方式为cat /usr/include/linux/input-event-codes.h
比如设置摇杆上为 BTN_FORWARD,摇杆下为BTN_BACK

evdev:input:b*v248Ap8266*
 KEYBOARD_KEY_c00e9=0x115
 KEYBOARD_KEY_c00ea=0x116

保存设置

sudo udevadm hwdb --update
sudo udevadm control --reload
sudo udevadm trigger

重新连接设备