2

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.

Dania
  • 1,648
  • 4
  • 31
  • 57

2 Answers2

2

"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

Jbobo Lee
  • 33
  • 4
  • Thanks, but I tried to execute the command to check which event node the device uses, but I got no clear output. I tried that in both runtime.getruntime.exec and adb shell, can you please clarify how to execute the commands in the second link? – Dania Apr 01 '16 at 08:46
  • 1
    Also, for touch event, you really need to make sure the input even fields are 100% correct. (It is device dependent. ) I only use it with the "recorded events" and play it back. Other wise, the info likely to be incorrect. – Jbobo Lee Apr 02 '16 at 16:57
  • Thanks, for the root privilege, I need to have su key word before the command and the device must be rooted right? Regarding the second point, do you mean that I need to know whether it's event2 or event4, etc.? – Dania Apr 02 '16 at 17:16
1

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.

gMale
  • 17,147
  • 17
  • 91
  • 116
  • Thanks a lot, but I'm looking to a linux command because I want the android app itself to inject the events without the need to connect it to pc, is this possible? Or, is there a library to run adb commands within app? – Dania Mar 31 '16 at 19:58