7

Imagine that I have a service that receives coordinates from a bluetooth device, now I want to display a mouse cursor whenever it moves.

I managed to send MotionEvents with a toolType = TOOL_TYPE_MOUSE but I don't get the native android mouse cursor displayed on screen.

The events I am sending look like these:

05-14 13:38:05.043: I/onTouchEvent(30301): MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=498.0, y[0]=996.0, toolType[0]=TOOL_TYPE_MOUSE, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=251957430, downTime=251957420, deviceId=1, source=0x2002 }

I am creating them like this:

  public void moveMouse(int x, int y)   {

        // Obtain MotionEvent object
        long downTime = SystemClock.uptimeMillis() - 10;
        long eventTime = SystemClock.uptimeMillis();
        int metaState = 0;
        MotionEvent.PointerProperties p = new PointerProperties();
        p.toolType = MotionEvent.TOOL_TYPE_MOUSE;
        p.id = 0;
        MotionEvent.PointerProperties[] properties = {p};

        MotionEvent.PointerCoords c = new MotionEvent.PointerCoords();
        c.x = x;
        c.y = y;
        c.orientation = 0f;
        c.pressure = 1f;
        c.size = 1f;

        MotionEvent.PointerCoords[] coords = {c};
        int buttonState = 0;
        float precisionX = 1.0f;
        float precisionY = 1.0f;
        int deviceId = 1;
        int edgeFlags = 0;
        int flags = 0;

        MotionEvent event;
        event =
                MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, properties,
                        coords, metaState, buttonState, precisionX, precisionY, deviceId,
                        edgeFlags, InputDevice.SOURCE_MOUSE, flags);

    dispatchTouchEvent(event);

But I still can't see the mouse pointer. What am I doing wrong?

Fernando Gallego
  • 4,064
  • 31
  • 50
  • Apparently, simulating a mouse through mouse events is not enough to trigger the native mouse pointer – Fernando Gallego May 19 '14 at 09:29
  • It appears that the mouse cursor is managed by InputReader (probably https://android.googlesource.com/platform/frameworks/native/+/nougat-release/services/inputflinger/InputReader.cpp#2635), which feeds InputDispatcher (one-way -- https://android.googlesource.com/platform/frameworks/native/+/nougat-release/services/inputflinger/InputManager.h#38). To trigger the mouse cursor the events would have to be handled at the lowest level, as a fake mouse device, rather than just being added to the input event queue. – fadden Nov 23 '16 at 20:24
  • @FernandoGallego Heyy, did you find any solution? – shinilms Dec 05 '17 at 07:39
  • @fadden Any idea on how to fake a mouse device on lowest level? – shinilms Dec 05 '17 at 07:41
  • Have you find a solution yet? – Jaswant Singh Sep 09 '20 at 16:16
  • @JaswantSingh nope, that was 6 years ago – Fernando Gallego Sep 11 '20 at 08:08

0 Answers0