如何将触摸事件直接发送到/dev/input/event



我想在整个Android系统中发送触摸事件。我正在从后台服务发送事件。我的设备已扎根,我已将应用程序存储在/system/app中。

我尝试了instrumentation and system/bin/input tab,在应用程序之外检测不起作用,第二个没有生成错误,但什么也没做。我尝试过直接注入/dev/input/event2,但没有效果。

这是代码:

 Thread t = new Thread(new Runnable() {
    @Override
    public void run() {


               try {
                    Process process = Runtime.getRuntime().exec("su");//supolicy --live "allow appdomain input_device dir { ioctl read getattr search open }" "allow appdomain input_device chr_file { ioctl read write getattr lock append open }"");
                    Runtime.getRuntime().exec ("su chmod 666 /dev/input/event2");
                   DataOutputStream os = new DataOutputStream(process.getOutputStream());
                    String cmd = "su /dev/input/event2  ABS_X "+  xPos + " ABS_Y "+ yPos+ "n";
                    Runtime.getRuntime().exec(cmd);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

我使用此网站作为参考:https://yhcting.wordpress.com/2010/11/29/linux-writing-input-event-directly/,但我不确定我是否正确使用了它。如何在/dev/input/event2中注入事件?

另外,什么是/dev/input/eventX?我总是看到它,但不清楚它是一个输入节点,还是只是事件节点的一般指示。最后,我如何知道应该将事件发送到哪个事件节点(event1、event2等)?

编辑:我通过adb shell尝试了su/dev/input/event2 ABS_X xPos ABS_Y yPos,我收到一行字说:未知用户名或uid,这是什么意思?

谢谢。

"input"只是android系统中的一个sh脚本。

# Script to start "input" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/input.jar
exec app_process $base/bin com.android.commands.input.Input $*

关于"输入"是如何实现的跟踪Java代码可以在这里找到:

http://www.srcmap.org/sd_share/4/aba57bc6/AOSP_adb_shell_input_Code_Trace.html

您还可以使用"sendevent"直接输入/dev/input/event0 来注入触摸事件

http://ktnr74.blogspot.com/2013/06/emulating-touchscreen-interaction-with.html

我对/dev/input/event2了解不多。然而,为了解决第一句话中的主要目标,如果外壳上有adb,则可以通过adb发送触摸事件,命令如下:

# send the text "password1" into the currently selected textview
adb shell input text password1
# send the enter key
adb shell input keyevent 66

有关触摸设备的更多信息,请点击此处。这是一个有用的Android键代码列表。

最新更新