如何使用 ADB 在 android 上分别发送 key down 和 key up 事件? [英] How to send key down and key up events separately on android using ADB?

查看:39
本文介绍了如何使用 ADB 在 android 上分别发送 key down 和 key up 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个回答了关于在 android 上发送长按的问题,问题在于

  • 没有一个答案实际上有效,有些使用过时的 API,有些 API 太新了
  • 我需要发送按键按下事件,然后才决定解除按键

从链接问题的最高投票答案开始,我有以下代码:

From the top voted answer on linked question I have this code to start with:

adb shell sendevent /dev/input/event2 1 172 1
adb shell sendevent /dev/input/event2 0  0 0
timeout 1
adb shell sendevent /dev/input/event2 1 172 0
adb shell sendevent /dev/input/event2 0 0 0

这些命令不起作用,答案的作者也没有提及命令的作用和原因.通过一些实验,我发现:

These commands do not work, the author of the answer also didn't drop a single word about what the commands do and why. Through some experiments I figured out that:

  • 必须是 /dev/input/event0 而不是 event2
  • 更改任何数字都不会发生任何事情
  • adb shell sendevent/dev/input/event2 0 0 0 是必需的.我不知道它有什么作用
  • It must be /dev/input/event0 instead of event2
  • Changing any of the numbers results in nothing happening
  • adb shell sendevent /dev/input/event2 0 0 0 is required. What does it do I don't know

现在我需要为包括不在设备上的键在内的键发送键向上/向下事件(例如 KEYCODE_DPAD_DOWN),所以 adb shell getevent 没有多大帮助 - 我不能按没有被按下.我正在使用 Android 4.1.2 三星手机.

Now I need to send key up/down events for keys including those that are not on the device (for example KEYCODE_DPAD_DOWN), so adb shell getevent doesn't help that much - I can't press keys that are not there to be pressed. I'm working with Android 4.1.2 Samsung phone.

还有谁能向我解释一下什么是 0 0 0 事件?

Also can anyone explain to me what is the 0 0 0 event?

推荐答案

我很久以前做过这个,所以我不记得所有的细节.希望我不会错过任何重要的...
设备上存在的所有输入硬件都映射到文件中(它是 Linux,所以当然一​​切都是文件......).如果你执行 adb shell ls/dev/input 你会看到很少的 eventX 文件,从 event0 开始.这些文件被映射到屏幕(触摸屏,而不是显示器!通常是 event2)、硬键(通常是 event0)、接近传感器和所有其他传感器/设备的输入.
要记住的另一件事是这些不是普通"文件,而是字符文件"——它是一种缓冲区,只有在相关输入设备被激活时才具有数据.您可以输入 adb shell cat/dev/input/event0 直到您按下设备上的某个物理键(例如 home 或 <代码>卷起来.当您按下某个键时,您会在屏幕上看到一些乱码,但该乱码当然是有意义的.
输入adb pull/dev/input/event0,按下并松开vol up,然后按下ctrl-c停止pull 命令.现在你将有一个名为 event0 的二进制文件,所以用一些十六进制编辑器打开它,你会得到类似 -

I've done this long time ago, so I don't remember all the details. Hope I don't miss anything crucial...
All the input hardware that exist on the device are mapped into files (it's Linux, so of course everything is a file...). If you do adb shell ls /dev/input you see few eventX files, from event0 and up. These files are mapped to the screen (the touch screen, not the display! which is usually event2), hard keys (usually event0), proximity sensor and all the other sensors/inputs of the device.
Another thing to bare in mind is these are not "normal" files, but "character files" - it's kind of a buffer that has data only at the moment that the relevant input device is activated. You can type adb shell cat /dev/input/event0 and you won't see a thing until the moment you press some physical key on your device, like home or vol up. When you press a key, you see some gibrish on your screen, but that gibrish has meaning of course.
Type adb pull /dev/input/event0, press and release the vol up and press ctrl-c to stop the pull command. Now you'll have a binary file called event0, so open it with some Hex editor and you'll get something like -

59 07 00 00 80 C9 07 00 01 00 18 00 01 00 00 00
59 07 00 00 80 C9 07 00 00 00 00 00 00 00 00 00
5A 07 00 00 96 60 01 00 01 00 18 00 00 00 00 00
5A 07 00 00 96 60 01 00 00 00 00 00 00 00 00 00

59 07 00 00 80 C9 07 00 01 00 18 00 01 00 00 00
59 07 00 00 80 C9 07 00 00 00 00 00 00 00 00 00
5A 07 00 00 96 60 01 00 01 00 18 00 00 00 00 00
5A 07 00 00 96 60 01 00 00 00 00 00 00 00 00 00

每行的前 8 个字节是事件的时间戳,其他 8 个字节是事件本身.
第一行按下vol.向上键(小端):
01 00 是事件的 ID.如果你同时按下两个键,你会得到两个带有两个 ID 的事件.
18 00关键代码.
01 00 是事件类型.01 是按下,00(如第 3 行)是释放.
00 00(最后两个字节)在这里没有使用.例如,对于屏幕触摸,它保存屏幕坐标.
第二行是一些包含全零的缓冲区清理/同步事件.
第三行正在释放钥匙.与第一行相同,但事件类型为00.
第四行再次是同步事件.
现在,如果您创建一个名为 eventFile 的文件,其中包含前两行,那么您实际上有一个按 vol 的序列.向上键.您可以使用 - adb push eventFile/dev/input/event0 将其注入设备,您将看到 vol.向上 键被按下但未释放 - 长按.要释放它,只需发送带有其他两行的另一个文件.您不必担心时间戳,您可以保持原样.
对于不同的键,您当然必须更改键代码.
我已经在我的设备上尝试过它 - 三星 S3 mini/4.1.2 带有两个 vol up/down 键并且它工作正常.我没有尝试使用不存在的键盘键.

The first 8 bytes of each row are timestamp of the event and the other 8 bytes are the event itself.
The first row is pressing the vol. up key (little-endian):
01 00 is the ID of the event. If you press two keys simultaneously you get two events with two IDs.
18 00 is the key code.
01 00 is the event type. 01 is press, 00 (like line 3) is release.
00 00 (the last two bytes) are not in use here. For screen touch for example, it holds the screen coordinates.
The second row is some buffer cleaning/sync event that contains all zeros.
The third row is releasing the key. It's the same as the first row, but the event type is 00.
The fourth row is again the sync event.
Now, if you create a file called eventFile that contains the first two rows, you actually have a sequence of pressing the vol. up key. You can inject it to the device with - adb push eventFile /dev/input/event0 and you'll see that the vol. up key is pressed but not released - a long press. To release it, just send in another file with the other two lines. You don't have to worry about the time stamp, you can leave it as-is.
For different keys, you'll have to change the key code of course.
I've tried it on my device - Samsung S3 mini /4.1.2 with both vol up/down keys and it works. I didn't try it with non-existing keypad keys.

这篇关于如何使用 ADB 在 android 上分别发送 key down 和 key up 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆