How to enter input events in native Android application?

I am looking for a way to input input events, such as touch or keyboard events, into my own application, from the code in this particular application. NOT on Android OS or other b / c applications that require permission at the signature level android.permission.INJECT_EVENTS or an embedded device, and I should avoid both. I am looking for code that runs on an unloaded device without any elevated privileges.

I am somehow progressing a bit with code based on the following concept:

View v = findViewById(android.R.id.content).getRootView();
v.dispatchTouchEvent(e);
+4
source share
2 answers

I think getRootView and submit will work well if there are any problems with it. Please just report it.

, . View.java api, getViewRootImpl(), android.view.ViewRootImpl . enqueueInputEvent ( InputEvent). , .

, , , , . . ACTION_POINTER_DOWN properties. , , . , [i].id, . .

        for (int i = 0; i < size; i++) {
            properties[i].id = fingerId;
            properties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;

            coordses[i].x = currentX;
            coordses[i].y = currentY;
            coordses[i].pressure = 1;
            coordses[i].size = DEFAULT_SIZE;
        }
        event = MotionEvent.obtain(
                    mDownTime, SystemClock.uptimeMillis(),
                    ((size - 1) << MotionEvent.ACTION_POINTER_INDEX_SHIFT)
                            + MotionEvent.ACTION_POINTER_DOWN,
                    size,
                    properties, coordses, DEFAULT_META_STATE, DEFAULT_BUTTON_STATE,
                    DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,
                    DEFAULT_EDGE_FLAGS, InputDevice.SOURCE_TOUCHSCREEN, DEFAULT_FLAGS);
+1

Source: https://habr.com/ru/post/1659934/


All Articles