当前位置: 首页 > news >正文

【Android】sendevent和getevent

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

sendevent的代码

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

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的显示来观察

一个点击

/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

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

/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个点呢

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


http://www.mrgr.cn/news/28865.html

相关文章:

  • 第3篇 滑动开关控制LED__ARM汇编语言工程<一>
  • Vue页面假死点不动现象Cannot read properties of undefined(reading ‘_wrapper‘)报错
  • leetcode86:分隔链表
  • arm 汇编技巧
  • 5G网卡network connection: disconnected
  • 使用Docker快速部署FastAPI Web应用
  • day21JS-axios数据通信
  • osg中显示3dtiles和cesiumIon
  • 一键更换软件源的工具——chsrc
  • fiddler抓包02_安装
  • Chainlit集成LlamaIndex并使用通义千问模型实现AI知识库检索网页对话应用增强版
  • 经典sql题(七)查找直播间最大在线人数
  • 【算法】差分思想:强大的算法技巧
  • 【补充篇】Davinci工具要求的dbc格式
  • 访谈心脑血管名医黄力医生:医术精湛,心系患者
  • 如何提高网站搜索排名
  • AI大模型
  • 98-策略模式的理解
  • 240. 搜索二维矩阵 II
  • 【OJ刷题】双指针问题6
  • 从0-1 用AI做一个赚钱的小红书账号(不是广告不是广告)
  • PowerShell脚本编写:自动化Windows开发工作流程
  • 网络安全笔试进阶练习题,来测测你能答对几个?
  • 正版138JAVA部署mysql5.5.JDK环境
  • 经典sql题(八)SQL 查询详细指南总结一
  • C++中的new与delete