【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博客

相关推荐
晨曦_子画4 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
孤客网络科技工作室26 分钟前
AJAX 全面教程:从基础到高级
android·ajax·okhttp
Mr Lee_2 小时前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野2 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&2 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
Winston Wood4 小时前
Android Parcelable和Serializable的区别与联系
android·序列化
清风徐来辽4 小时前
Android 项目模型配置管理
android
帅得不敢出门5 小时前
Gradle命令编译Android Studio工程项目并签名
android·ide·android studio·gradlew
problc5 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
帅得不敢出门16 小时前
安卓设备adb执行AT指令控制电话卡
android·adb·sim卡·at指令·电话卡