【Android】sendevent和getevent

在之前我们讨论了通过input命令,外接输入设备(鼠标)等方式来实现对屏幕的事件注入,达到实现一些自动化的操作,相对于input命令需要获取inputManager来进行操作,sendevent的方式直接写文件来注入粗糙的事件,调用起来清楚一些,就是指令复杂一些,

sendevent的代码

/external/toybox/toys/android/sendevent.c

cpp 复制代码
1/* sendevent.c - Send Linux input events.
2 *
3 * Copyright 2016 The Android Open Source Project
4
5USE_SENDEVENT(NEWTOY(sendevent, "<4>4", TOYFLAG_USR|TOYFLAG_SBIN))
6
7config SENDEVENT
8  bool "sendevent"
9  default y
10  depends on TOYBOX_ON_ANDROID
11  help
12    usage: sendevent DEVICE TYPE CODE VALUE
13
14    Sends a Linux input event.
15*/
16
17#define FOR_sendevent
18#include "toys.h"
19
20#include <linux/input.h>
21
22void sendevent_main(void)
23{
24  int fd = xopen(*toys.optargs, O_RDWR);
25  int version;
26  struct input_event ev;
27
28  if (ioctl(fd, EVIOCGVERSION, &version))
29    perror_exit("EVIOCGVERSION failed for %s", *toys.optargs);
30
31  memset(&ev, 0, sizeof(ev));
32  // TODO: error checking and support for named constants.
33  ev.type = atoi(toys.optargs[1]);
34  ev.code = atoi(toys.optargs[2]);
35  ev.value = atoi(toys.optargs[3]);
36  xwrite(fd, &ev, sizeof(ev));
37}

就是写数据到输入设备。

复杂的就在于写入的数据,我们可以通过getevent -l的显示来观察

一个点击

bash 复制代码
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   000079a5
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    00000142
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    00000274
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000006
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event7: EV_KEY       BTN_TOOL_FINGER      DOWN
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000005
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000000
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   ffffffff
/dev/input/event7: EV_KEY       BTN_TOUCH            UP
/dev/input/event7: EV_KEY       BTN_TOOL_FINGER      UP
/dev/input/event7: EV_SYN       SYN_REPORT           00000000

手指再滑动一下,看看打印什么,

bash 复制代码
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   000079a8
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000ee
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001ad
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000004
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event7: EV_KEY       BTN_TOOL_FINGER      DOWN
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000005
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001ae
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001af
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001b2
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000ef
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001b5
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001b9
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000f0
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001bd
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001c1
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001c4
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001c8
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001cc
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000f1
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001d1
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001d6
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000f2
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001db
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000f3
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001df
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001e3
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001e6
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001e8
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001e9
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    000000f4
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    000001ea
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000000
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   ffffffff
/dev/input/event7: EV_KEY       BTN_TOUCH            UP
/dev/input/event7: EV_KEY       BTN_TOOL_FINGER      UP
/dev/input/event7: EV_SYN       SYN_REPORT           00000000

可以看到有很多ABS_MT_POSITION_X, ABS_MT_POSITION_Y 的变化

如果是2个点呢

bash 复制代码
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   000079aa
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    0000010f
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    0000017f
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000001
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event7: EV_KEY       BTN_TOOL_FINGER      DOWN
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000002
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000003
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000001
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000004
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000005
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000001
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   000079ab
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    0000015c
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    00000315
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000002
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000001
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000004
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000001
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000005
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_SYN       SYN_REPORT           00000000
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000000
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000002
/dev/input/event7: EV_ABS       ABS_MT_SLOT          00000001
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MINOR   00000003
/dev/input/event7: EV_SYN       SYN_REPORT           00000000

可以看到,使用了ABS_MT_SLOT 来区分2个点,

sendevent命令通常在具有root权限的环境中运行,因为它需要直接访问输入设备文件。在Android设备上,这些设备文件通常位于/dev/input/eventX目录下,其中X是一个数字,表示不同的输入设备。

以下是如何使用sendevent的基本步骤:

使用方法

  1. 确定设备文件 : 首先,你需要找到你要发送事件的目标设备的设备文件。可以使用ls /dev/input/event*来查看所有可用的输入设备文件。还可以使用cat /proc/bus/input/devices来获取更多关于输入设备的信息,包括设备文件名、设备类型等。

  2. 使用sendevent : 一旦找到了目标设备文件,就可以使用sendevent命令来发送事件了。命令的基本格式如下:

    1sendevent /dev/input/eventX type code value
    

    其中:

    • /dev/input/eventX:目标设备的设备文件路径。
    • type:事件类型,例如EV_KEY(按键事件)、EV_REL(相对运动事件)、EV_ABS(绝对运动事件)等。
    • code:事件代码,对应特定类型的事件。
    • value:事件值,具体含义取决于事件类型和代码。

示例

假设你想模拟一次触摸事件,可以这样做:

  1. 查找触摸屏设备文件:

    1ls /dev/input/event*
    2cat /proc/bus/input/devices | grep "Handlers"
    
  2. 假设触摸屏设备文件为/dev/input/event0,并且你想模拟一次触摸事件,你可以发送一个触摸开始(ABS_MT_POSITION_XABS_MT_POSITION_Y)和结束(BTN_TOOL_FINGER)的事件:

    1sendevent /dev/input/event0 0 0 0 # 清空之前的事件
    2sendevent /dev/input/event0 EV_ABS ABS_MT_POSITION_X 100 # 触摸点X坐标
    3sendevent /dev/input/event0 EV_ABS ABS_MT_POSITION_Y 200 # 触摸点Y坐标
    4sendevent /dev/input/event0 EV_SYN SYN_REPORT 0 # 发送同步信号
    5sendevent /dev/input/event0 EV_KEY BTN_TOOL_FINGER 1 # 模拟触摸开始
    6sendevent /dev/input/event0 EV_SYN SYN_REPORT 0 # 发送同步信号
    7sendevent /dev/input/event0 EV_KEY BTN_TOOL_FINGER 0 # 模拟触摸结束
    8sendevent /dev/input/event0 EV_SYN SYN_REPORT 0 # 发送同步信号
    

参考资料:

Android getevent/sendevent/input 详解_android sendevent-CSDN博客

相关推荐
Dnelic-2 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen5 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年12 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿14 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神15 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛16 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法16 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter17 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快18 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl19 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5