Mouse position OTG Android

Hi, I am struggling with this problem for some time and I have no idea how to solve it.

I am a bit basic if it comes to the Java. I am using Unity to develop my app and I have problem with mouse connected to the android device via OTG usb. The problem is:

  • When I move the mouse pointer on the edge of the screen then I cannot get a mouse delta position, it always return 0 (but I really need to know a mouse delta position all the time even when the mouse pointer reach a screen edge).

On PC on other hand it works fine even if your mouse pointer reach edge you still can read the mouse delta position here is a video:

https://streamable.com/bhipjy

I was trying to read it by

Input.mousePosition
Input.GetAxis("Mouse X");
Input.GetAxisRaw("Mouse X");

I tried also:

Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.Confined;

But none of these work on android, receiving of mouse delta position stops after mouse pointer reach the edge of the screen.

Here is a video:
https://streamable.com/q9thgd

When the cursor reached the edge I was still moving it physically and no delta X changed.

I was trying to experiment with making an android lib because I have read somewhere that you can get an access to the mouse pointer data by setOnGenericMotionListener or something like that. But I don’t know how to achieve that. Here is my code. It doesn’t blow up after calling SetListener because I get a toast message.

package com.test.mouselib;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import android.util.Log;
import android.os.Looper;

public class MouseTest extends Application
{
    public void SetListener(final Activity ctx, final String message)
    {
        View view = ctx.getCurrentFocus();
        view.setOnGenericMotionListener(new View.OnGenericMotionListener() {
            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {
                last = event.toString();
                return false;
            }
        });
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Hey it has been a few weeks since this post - did you get it working?

I tried the same code you provided here and plug it in Unity with the activity ctx set in Unity script as:

    AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");

    pluginClass.Call("SetListener", currentActivity, dbgMsg);

This works well for me to hook up the callback set in SetListner.

Hope this helps!

Hi,
I achieved by use of native plugin https://github.com/android/games-samples/tree/main/googleplaygamesforpc/unity_projects/input_capture_package