4

I'm developing an android app that needs to send touch events to /dev/input/eventX. I know the C code structure to do such a thing is the following:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

To use such a code, I need to set NDK. Instead, I want to run equivalent linux commands using Runtime.getRunTime.exec () in android without the need to use NDK. Is there any way to do that?

If no, what are the rest of C code needed to send an event? For example, how can I send a touch event at x=200 and y=300 to event0? I searched and I didn't find a clear solution.

Thanks.

Dania
  • 1,648
  • 4
  • 31
  • 57
  • Unless you are running on a rooted device or your own custom ROM, your app cannot generate fake input data. – CommonsWare Apr 02 '16 at 11:13
  • 1
    @CommonsWare thanks, my device is rooted, how can I do that? – Dania Apr 02 '16 at 11:14
  • When you used a search engine to search `android rooted device fake user input`, what did you learn? – CommonsWare Apr 02 '16 at 11:16
  • @CommonsWare I've been searching and testing for more than 3 weeks. I found instrumentation and I tried that, it failed outside the context of app. I tried /system/bin/input tab x y, but it did nothing. I tried sendevent /dev/input/eventX and it did nothing too. Then, I found that writing to /dev/input/eventX needs c code, but I don't want to use NDK, I want to use linux commands in my app, and I didn't find such commands. Please help me, how to do that? – Dania Apr 02 '16 at 11:26
  • "I tried /system/bin/input tab x y, but it did nothing. I tried sendevent /dev/input/eventX and it did nothing too" -- those are the classic solutions AFAIK. Make sure you are executing them as superuser, using whatever it is that you have for that. You might hunt around for existing source code that uses those commands. – CommonsWare Apr 02 '16 at 11:39
  • @CommonsWare To run them as super user I must add a su before the command right? I did that and it didn't work. Just to emphasize I need to inject touch event not to fake it. Some of these methods where really touching the screen, but there was no action after the touch. I mean, I want a method to provide an touch input just like that provided by the user, is this possible? – Dania Apr 02 '16 at 11:44
  • @CommonsWare There is a library in this link http://www.pocketmagic.net/programmatically-injecting-events-on-android-part-2/, the developer says that it can inject events, but I couldn't know how to use it. That's why I asked for linux commands, do you know anyway to solve this problem? – Dania Apr 02 '16 at 11:46
  • 2
    Link related answer: http://stackoverflow.com/a/28845362/3866447 – Sam Protsenko Apr 02 '16 at 14:55
  • @SamProtsenko thanks a lot, I'll check it. – Dania Apr 02 '16 at 16:53

1 Answers1

3

I'm not clear about why you need to send event to /dev/input/eventX directly. But if it can send via adb, you can inject many type events to device.

Try this on your computer:

adb shell input tap 200 300

Or this on your android device shell:

input tap 200 300

But it has a high delay because of outside injection.

More info about input command see here.

Usage: input [<source>] <command> [<arg>...]

The sources are: 
      mouse
      keyboard
      joystick
      touchnavigation
      touchpad
      trackball
      stylus
      dpad
      touchscreen
      gamepad

The commands and default sources are:
      text <string> (Default: touchscreen)
      keyevent [--longpress] <key code number or name> ... (Default: keyboard)
      tap <x> <y> (Default: touchscreen)
      swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
      press (Default: trackball)
      roll <dx> <dy> (Default: trackball)
sakiM
  • 4,832
  • 5
  • 27
  • 33