Dania
Dania

Reputation: 1688

How to directly send touch events to /dev/input/event?

I want to send touch events throughout Android system. I'm sending events from a background service. My device is rooted and I've stored my app in /system/app.

I tried instrumentation and system/bin/input tab, instrumentation didn't work outside the app, and the second one doesn't generate an error, but does nothing. I've tried injecting directly to /dev/input/event2, but I get no effect.

Here is the code:

 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();

I used this website as a reference: https://yhcting.wordpress.com/2010/11/29/linux-writing-input-event-directly/, but I'm not sure if I'm applying it correctly. How can I inject events in /dev/input/event2?

Also, what is /dev/input/eventX? I always see it, but it's not clear whether it's an input node or it's just a general indication of the event node. Finally, how can I know to which event node (event1, event2, etc.) should I send the event?

EDIT: I tried su /dev/input/event2 ABS_X xPos ABS_Y yPos via adb shell I got a line saying: unknown username or uid, what does that mean?

Thanks.

Upvotes: 2

Views: 8249

Answers (2)

Jbobo Lee
Jbobo Lee

Reputation: 43

"input" is just a sh script in android system.

# 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 $*

Trace the Java code on how "input" is implemented can be found here:

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

You can also use "sendevent" to inject touch events directly input /dev/input/event0

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

Upvotes: 3

gMale
gMale

Reputation: 17895

I don't know much about /dev/input/event2 However, to address the primary objective in your first sentence, if you have adb available on the shell, you can send touch events via adb with commands like:

# send the text "password1" into the currently selected textview
adb shell input text password1

# send the enter key
adb shell input keyevent 66

More information for touch devices can be found here. And this is a helpful list of Android KeyCodes.

Upvotes: 1

Related Questions