4

I've been trying to figure out how to inject touch/ keyboard events into an Android device for a while now (within and outside of you application).

I found an app that does this without root permissions:

https://play.google.com/store/apps/details?id=com.vmlite.vncserver

Does anyone have any clue how they did it?

Habeeb Ahmed
  • 217
  • 4
  • 9
  • this is a pretty broad question. You might have a better response theorizing this on reddit.com/r/androiddev – cbrulak Jan 29 '14 at 15:27

2 Answers2

3

If you want to inject touch events on android app without root:

you can use Instrumentation class, https://developer.android.com/reference/android/app/Instrumentation.html

import android.app.Instrumentation;

public class MainActivity extends Activity {
Instrumentation m_Instrumentation;
  public void onCreate(Bundle savedInstanceState) {
    m_Instrumentation = new Instrumentation();

    int x=0; //your x coord in screen. 
    int y=0; // your y coord in screen. 
    m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,x, y,0));
            m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,x, y,0));
}
}

This method obtains ACTION_DOWN and ACTION_UP event which injects an event on the current layout.

Note: if the injection of your coordinates (x,y) is outside of screen size, the app will crash. This method injection works only inside app, if you want to inject touch events, you need a rooted device and inject events through adb command.

vgonisanz
  • 11,831
  • 13
  • 78
  • 130
uelordi
  • 2,189
  • 3
  • 21
  • 36
2

for non-rooted devices, every time after turning the device completely off and on, you will have to connect your device to a Windows PC or Mac using a USB cable, then run a free desktop program, VMLite Android App Controller, to start the server on your device.

I'm quite sure that at this step it changes the permissions on /dev/input/event.. files and performs the injecting by writing those (sidestepping the Android VM). This technique is detailed in this blog post: Part1, Part2

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
  • I need to build a similar app, can you please tell me what kind of server is needed to change the privileges of writing events? It's written that it uses VNC protocol to achieve remote controlling of the device, but how can we change writing events permission without root? Thanks. – Dania Jan 15 '16 at 18:13
  • @Dania, not sure what you are asking. You change the permissions on the system level so that user code can write to the event "files" (which are not files but I/O works on them due to the abstraction that linux provides). – Tamás Szelei Jan 16 '16 at 21:49
  • 1
    thanks a lot for your comment. I'm asking how can we access /dev/input/event and change the writing permissions using the application, and can I do that without rooting as in the application which is illustrated in the question? Because in the Part2 link you posted, the author states that we must root the device to change the events writing permission. So, I would like to know how we can change them without rooting as in the application in the question. Please let me know about your response. Thank you. – Dania Jan 17 '16 at 09:00