- Home /
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();
}
});
}
}
Answer by hctspheree · Mar 22, 2021 at 11:25 AM
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!
I haven't solved the problem yet.
But have you checked if it works fully? If it logs the mouse delta position?
yup - it works fine and returns delta in pixels. Just plug in a public field to get the mouse delta position via getAxisValue() and AXIS_RELATIVE_X or AXIS_RELATIVE_Y in your SetListerner function. Make sure you have api level 24 and above to read the relative values.
Could you share the whole code? I mean Unity and Java? To show everyone a clear answer for a future? :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613025933im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to find a way to detect if a mouse is plugged in 1 Answer
Get mouse position outside of the main window 0 Answers
How to replace the mouse with a 3D model? 1 Answer
Mouse follow on network 0 Answers
How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers