Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject a touch event #5473

Open
bpn2k4 opened this issue Nov 12, 2024 · 0 comments
Open

How to inject a touch event #5473

bpn2k4 opened this issue Nov 12, 2024 · 0 comments

Comments

@bpn2k4
Copy link

bpn2k4 commented Nov 12, 2024

Hi, i follow Controller.java to inject a touch on screen. Here is my code :

import android.annotation.SuppressLint;
import android.os.SystemClock;
import android.view.InputEvent;
import android.view.MotionEvent;

import java.lang.reflect.Method;

@SuppressLint("PrivateApi,DiscouragedPrivateApi")
public class Touch {

  private Method injectInputEventMethod;
  private Object inputManagerInstance;

  public Touch() {
    try {
//      Class<?> inputManagerClass = Class.forName("android.hardware.input.InputManager");
      Class<?> inputManagerClass = android.hardware.input.InputManager.class;
      Method getInstanceMethod = inputManagerClass.getDeclaredMethod("getInstance");
      this.inputManagerInstance = getInstanceMethod.invoke(null);
      this.injectInputEventMethod = inputManagerInstance.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);
      if (this.injectInputEventMethod == null) {
        System.out.println("Can't get injectInputEventMethod");
      }
      else {
        System.out.println("injectInputEventMethod: " + this.injectInputEventMethod.getName());
      }
    } catch (Exception e) {
      e.printStackTrace(System.out);
    }
  }

  public void touch() {
    try {
      float x = 640.0F;
      float y = 360.0F;
      System.out.println("Touch (" +  (int) x + ", " + (int) y + ")");

      final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
      pointerProperties[0] = new MotionEvent.PointerProperties();
      pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;

      final MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
      pointerCoords[0] = new MotionEvent.PointerCoords();
      pointerCoords[0].orientation = 0.0F;
      pointerCoords[0].pressure = 1.0F;
      pointerCoords[0].x = x;
      pointerCoords[0].y = y;

      long downTime = SystemClock.uptimeMillis();
      MotionEvent motionEventDown = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, 0, 0, 4098, 0);
      boolean result = (boolean) this.injectInputEventMethod.invoke(this.inputManagerInstance, motionEventDown, 0);
      System.out.println("[Debug] injectTouch result=" + result);

      long eventTime = SystemClock.uptimeMillis();
      MotionEvent motionEventUp = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, 0, 0, 4098, 0);
      result = (boolean) this.injectInputEventMethod.invoke(this.inputManagerInstance, motionEventUp, 0);
      System.out.println("[Debug] injectTouch result=" + result);
    }
    catch (Exception e) {
      System.out.println("Can't not touch");
      e.printStackTrace(System.out);
    }
  }
}

Then i call it:

Touch touch = new Touch();
touch.touch();

But nothing happen, here is the log:

D:\tutorial\server>adb -s emulator-5554 shell CLASSPATH=/data/local/tmp/app.jar app_process / com.capture.Main
injectInputEventMethod: injectInputEvent
Touch (640, 360)
[Debug] injectTouch result=false
[Debug] injectTouch result=false

My emulator is LDPlayer using android 9. I have test success with scrcpy for window but my code didn't work.
What problem in my code?
Thanks for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant