Linux下查看输入设备、获取输入事件的详细方法

/dev/input/目录

/dev/input/目录下的事件都是在驱动中调用input_register_device(struct input_dev *dev)产生的。笔者的/dev/input/目录中的文件如下:

$ ls /dev/input/
by-id  by-path  event0  event1  event10  event11  event12  event13  event2  event3  event4  event5  event6  event7  event8  event9  mice  mouse0  mouse1  mouse2
$ ls -l /dev/input
总用量 0
drwxr-xr-x 2 root root     100 11月  7 11:09 by-id
drwxr-xr-x 2 root root     200 11月  7 11:09 by-path
crw-rw---- 1 root input 13, 64 11月  2 15:53 event0
crw-rw---- 1 root input 13, 65 11月  2 15:53 event1
crw-rw---- 1 root input 13, 74 11月  2 15:53 event10
crw-rw---- 1 root input 13, 75 11月  2 15:53 event11
crw-rw---- 1 root input 13, 76 11月  2 15:53 event12
crw-rw---- 1 root input 13, 77 11月  2 15:53 event13
crw-rw---- 1 root input 13, 66 11月  2 15:53 event2
crw-rw---- 1 root input 13, 67 11月  2 15:53 event3
crw-rw---- 1 root input 13, 68 11月  2 15:53 event4
crw-rw---- 1 root input 13, 69 11月  7 11:09 event5
crw-rw---- 1 root input 13, 70 11月  2 15:53 event6
crw-rw---- 1 root input 13, 71 11月  2 15:53 event7
crw-rw---- 1 root input 13, 72 11月  2 15:53 event8
crw-rw---- 1 root input 13, 73 11月  2 15:53 event9
crw-rw---- 1 root input 13, 63 11月  2 15:53 mice
crw-rw---- 1 root input 13, 32 11月  2 15:53 mouse0
crw-rw---- 1 root input 13, 33 11月  2 15:53 mouse1
crw-rw---- 1 root input 13, 34 11月  7 11:09 mouse2

每个event代表一个事件。那么如何知道每个事件分别与哪个设备对应?这需要借助于/proc/bus了。

/proc/bus/input/devices

/proc/bus/input/devices存放了与event对应的相关设备信息。笔者电脑中的内容如下:

$ cat /proc/bus/input/devices 
I: Bus=0019 Vendor=0000 Product=0005 Version=0000
N: Name="Lid Switch"
P: Phys=PNP0C0D/button/input0
S: Sysfs=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1a/PNP0C0D:00/input/input0
U: Uniq=
H: Handlers=event0 
B: PROP=0
B: EV=21
B: SW=1

I: Bus=0019 Vendor=0000 Product=0001 Version=0000
N: Name="Power Button"
P: Phys=PNP0C0C/button/input0
S: Sysfs=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0C:00/input/input1
U: Uniq=
H: Handlers=kbd event1 
B: PROP=0
B: EV=3
B: KEY=10000000000000 0

I: Bus=0011 Vendor=0001 Product=0001 Version=ab41
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
S: Sysfs=/devices/platform/i8042/serio0/input/input2
U: Uniq=
H: Handlers=sysrq kbd event2 leds 
B: PROP=0
B: EV=120013
B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe
B: MSC=10
B: LED=7

I: Bus=0019 Vendor=0000 Product=0000 Version=0000
N: Name="Ideapad extra buttons"
P: Phys=ideapad/input0
S: Sysfs=/devices/pci0000:00/0000:00:1f.0/PNP0C09:00/VPC2004:00/input/input7
U: Uniq=
H: Handlers=rfkill kbd event6 
B: PROP=0
B: EV=13
B: KEY=81000800100c03 4400000000300000 0 2
B: MSC=10

I: Bus=0018 Vendor=06cb Product=ce2d Version=0100
N: Name="MSFT0001:01 06CB:CE2D Mouse"
P: Phys=i2c-MSFT0001:01
S: Sysfs=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-1/i2c-MSFT0001:01/0018:06CB:CE2D.0001/input/input8
U: Uniq=
H: Handlers=mouse0 event3 
B: PROP=0
B: EV=17
B: KEY=30000 0 0 0 0
B: REL=3
B: MSC=10

I: Bus=0018 Vendor=06cb Product=ce2d Version=0100
N: Name="MSFT0001:01 06CB:CE2D Touchpad"
P: Phys=i2c-MSFT0001:01
S: Sysfs=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-1/i2c-MSFT0001:01/0018:06CB:CE2D.0001/input/input9
U: Uniq=
H: Handlers=mouse1 event4 
B: PROP=5
B: EV=1b
B: KEY=e520 10000 0 0 0 0
B: ABS=2e0800000000003
B: MSC=20

I: Bus=0003 Vendor=13d3 Product=56ff Version=1919
N: Name="Integrated Camera: Integrated C"
P: Phys=usb-0000:00:14.0-7/button
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb3/3-7/3-7:1.0/input/input11
U: Uniq=
H: Handlers=kbd event7 
B: PROP=0
B: EV=3
B: KEY=100000 0 0 0

I: Bus=0019 Vendor=0000 Product=0006 Version=0000
N: Name="Video Bus"
P: Phys=LNXVIDEO/video/input0
S: Sysfs=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/LNXVIDEO:00/input/input12
U: Uniq=
H: Handlers=kbd event8 
B: PROP=0
B: EV=3
B: KEY=3e000b00000000 0 0 0

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sof-hda-dsp Headset Mic"
P: Phys=ALSA
S: Sysfs=/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0/input13
U: Uniq=
H: Handlers=event9 
B: PROP=0
B: EV=21
B: SW=10

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sof-hda-dsp Headphone"
P: Phys=ALSA
S: Sysfs=/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0/input14
U: Uniq=
H: Handlers=event10 
B: PROP=0
B: EV=21
B: SW=4

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sof-hda-dsp HDMI/DP,pcm=3"
P: Phys=ALSA
S: Sysfs=/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0/input15
U: Uniq=
H: Handlers=event11 
B: PROP=0
B: EV=21
B: SW=140

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sof-hda-dsp HDMI/DP,pcm=4"
P: Phys=ALSA
S: Sysfs=/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0/input16
U: Uniq=
H: Handlers=event12 
B: PROP=0
B: EV=21
B: SW=140

I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="sof-hda-dsp HDMI/DP,pcm=5"
P: Phys=ALSA
S: Sysfs=/devices/pci0000:00/0000:00:1f.3/skl_hda_dsp_generic/sound/card0/input17
U: Uniq=
H: Handlers=event13 
B: PROP=0
B: EV=21
B: SW=140

I: Bus=0003 Vendor=093a Product=2521 Version=0111
N: Name="USB OPTICAL MOUSE"
P: Phys=usb-0000:00:14.0-5/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb3/3-5/3-5:1.0/0003:093A:2521.0005/input/input20
U: Uniq=
H: Handlers=mouse2 event5 
B: PROP=0
B: EV=17
B: KEY=1f0000 0 0 0 0
B: REL=1943
B: MSC=10

可以看到,每一项的“H:”一行后边的内容中就是对应的event。以触摸板为例:

I: Bus=0018 Vendor=06cb Product=ce2d Version=0100
N: Name="MSFT0001:01 06CB:CE2D Touchpad"
P: Phys=i2c-MSFT0001:01
S: Sysfs=/devices/pci0000:00/0000:00:15.0/i2c_designware.0/i2c-1/i2c-MSFT0001:01/0018:06CB:CE2D.0001/input/input9
U: Uniq=
H: Handlers=mouse1 event4 
B: PROP=5
B: EV=1b
B: KEY=e520 10000 0 0 0 0
B: ABS=2e0800000000003
B: MSC=20

通过“N:”一行后边的内容可以看出是Touchpad设备,它对应的事件见于“H: Handlers=mouse1 event4”一行,是event4。

由此引出来一个常用的输入事件测试工具 —— evtest。

evtest

在开发input子系统驱动时,常常会使用evtest工具进行测试。evtest是打印evdev内核事件的工具,它直接从内核设备读取并打印设备描述的带有值和符号名的事件,可以用来调试鼠标、键盘、触摸板等输入设备
evtest通常用于在X.org中调试输入设备的问题,evtest提供了内核的处理信息。根据这些信息,可以判断是内核问题还是X.org问题。

输出数据中,“type”是input类型,可以是“EV KEY”、“EV SW”、“EV SND”、“EV LED”或数值;“value”可以是十进制也可以是十六进制,或者是查询的kev/开关/声音/LED的常量名。

下边看一下具体的安装使用方法。

安装

sudo apt install evtest -y

编译安装

$ tar   -xjvf   evtest_1.33.orig.tar.bz2
$ cd evtest-1.33/
$ ./configure
$ make
$ sudo make install

使用

sudo evtest /dev/input/eventx(x为事件编号)

 注:需要用root用户,或者sudo操作。

在笔者电脑中,运行示例如下:

$ sudo evtest

No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:	Lid Switch
/dev/input/event1:	Power Button
/dev/input/event2:	AT Translated Set 2 keyboard
/dev/input/event3:	MSFT0001:01 06CB:CE2D Mouse
/dev/input/event4:	MSFT0001:01 06CB:CE2D Touchpad
/dev/input/event5:	USB OPTICAL MOUSE
/dev/input/event6:	Ideapad extra buttons
/dev/input/event7:	Integrated Camera: Integrated C
/dev/input/event8:	Video Bus
/dev/input/event9:	sof-hda-dsp Headset Mic
/dev/input/event10:	sof-hda-dsp Headphone
/dev/input/event11:	sof-hda-dsp HDMI/DP,pcm=3
/dev/input/event12:	sof-hda-dsp HDMI/DP,pcm=4
/dev/input/event13:	sof-hda-dsp HDMI/DP,pcm=5
Select the device event number [0-13]: 

仍然以Touchpad为例,对应的event为4,输入4,并随便在触摸板上滑动,结果如下:

Select the device event number [0-13]: 4
Input driver version is 1.0.1
Input device ID: bus 0x18 vendor 0x6cb product 0xce2d version 0x100
Input device name: "MSFT0001:01 06CB:CE2D Touchpad"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 272 (BTN_LEFT)
    Event code 325 (BTN_TOOL_FINGER)
    Event code 328 (BTN_TOOL_QUINTTAP)
    Event code 330 (BTN_TOUCH)
    Event code 333 (BTN_TOOL_DOUBLETAP)
    Event code 334 (BTN_TOOL_TRIPLETAP)
    Event code 335 (BTN_TOOL_QUADTAP)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value    994
      Min        0
      Max     1224
      Resolution      12
    Event code 1 (ABS_Y)
      Value    505
      Min        0
      Max      804
      Resolution      12
    Event code 47 (ABS_MT_SLOT)
      Value      0
      Min        0
      Max        4
    Event code 53 (ABS_MT_POSITION_X)
      Value      0
      Min        0
      Max     1224
      Resolution      12
    Event code 54 (ABS_MT_POSITION_Y)
      Value      0
      Min        0
      Max      804
      Resolution      12
    Event code 55 (ABS_MT_TOOL_TYPE)
      Value      0
      Min        0
      Max        2
    Event code 57 (ABS_MT_TRACKING_ID)
      Value      0
      Min        0
      Max    65535
  Event type 4 (EV_MSC)
    Event code 5 (MSC_TIMESTAMP)
Properties:
  Property type 0 (INPUT_PROP_POINTER)
  Property type 2 (INPUT_PROP_BUTTONPAD)
Testing ... (interrupt to exit)
Event: time 1667899054.177672, type 3 (EV_ABS), code 57 (ABS_MT_TRACKING_ID), value 1515
Event: time 1667899054.177672, type 3 (EV_ABS), code 53 (ABS_MT_POSITION_X), value 376
Event: time 1667899054.177672, type 3 (EV_ABS), code 54 (ABS_MT_POSITION_Y), value 382
Event: time 1667899054.177672, type 1 (EV_KEY), code 330 (BTN_TOUCH), value 1
Event: time 1667899054.177672, type 1 (EV_KEY), code 325 (BTN_TOOL_FINGER), value 1

直接读取/dev/input/eventx

使用cat查看输入事件的内容,操作相应输入设备,事件会上报内容。以字符串方式解读会呈现乱码。如下所示:

sudo cat /dev/input/event4

b"jcW.	9�b"jcW.	5$b"jcW.	6�b"jcW.	Jb"jcW.	Eb"jcW.	$b"jcW.	�b"jcW.	b"jcW.	b"jc�6	X"jc�6	b"jc�S Rb"jc�S	b"jc�n	��b"jc�n	b"jco�	�b"jco�	b"jcަ	5'b"jcަ	6�b"jcަ	'b"jcަ	�b"jcަ	h�b"jcަ	b"jcU�	5+b"jcU�	6�b"jcU�	+b"jcU�	�b"jcU�	��b"jcU�	b"jci�	5/b"jci�	6�b"jci�	/b"jci�	�b"jci�	�b"jci�	b"jc��	54b"jc��	6�b"jc��	4b"jc��	�b"jc��	pb"jc��	b"jc�
58b"jc�
6�b"jc�
8b"jc�
�b"jc�

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝天居士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值