【问题标题】:How to directly send touch events to /dev/input/event?如何直接将触摸事件发送到/dev/input/event?
【发布时间】:2016-07-20 10:24:51
【问题描述】:

我想在整个 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 我收到一条信息:unknown username or uid,这是什么意思?

谢谢。

【问题讨论】:

    标签: android linux linux-kernel touchscreen


    【解决方案1】:

    “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”直接注入触摸事件 input /dev/input/event0

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

    【讨论】:

    • 谢谢,但我尝试执行命令检查设备使用哪个事件节点,但没有得到明确的输出。我在 runtime.getruntime.exec 和 adb shell 中都试过了,你能澄清一下如何执行第二个链接中的命令吗?
    • 另外,对于触摸事件,您确实需要确保输入偶数字段是 100% 正确的。 (它取决于设备。)我只将它与“录制的事件”一起使用并播放它。否则,信息可能不正确。
    • 谢谢,对于root权限,我需要在命令之前有su关键字,并且设备必须是root权限吗?关于第二点,你的意思是我需要知道是event2还是event4等?
    【解决方案2】:

    我对@9​​87654323@ 了解不多,但是,为了解决您第一句话中的主要目标,如果您在shell 上有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
    

    更多information for touch devices can be found here。这是一个有用的list of Android KeyCodes

    【讨论】:

    • 非常感谢,但我正在寻找一个 linux 命令,因为我希望 android 应用程序本身注入事件而无需将其连接到 pc,这可能吗?或者,是否有一个库可以在应用程序中运行 adb 命令?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多