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

相关推荐
m0_548514773 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯3 小时前
Android-性能优化-03-启动优化-启动耗时
android
凤邪摩羯4 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
喀什酱豆腐4 小时前
Handle
android
m0_748232926 小时前
Android Https和WebView
android·网络协议·https
m0_748251726 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
m0_748254668 小时前
go官方日志库带色彩格式化
android·开发语言·golang
zhangphil8 小时前
Android使用PorterDuffXfermode模式PorterDuff.Mode.SRC_OUT橡皮擦实现“刮刮乐”效果,Kotlin(2)
android·kotlin
爱学测试的李木子8 小时前
从0到1搭建 Android 自动化 python+appium 环境
android·软件测试·python·测试工具·自动化
咸芝麻鱼8 小时前
Android Studio | 连接手机设备后,启动App时出现:Waiting For DebuggerApplication (App名)...
android·adb·智能手机·android studio