Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wyrrhcyn · Feb 28, 2021 at 10:36 AM · androidmousemouseposition

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();
             }
         });
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wyrrhcyn · Mar 23, 2021 at 10:39 AM 0
Share

I haven't solved the problem yet.

But have you checked if it works fully? If it logs the mouse delta position?

avatar image hctspheree wyrrhcyn · Mar 23, 2021 at 03:11 PM 0
Share

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.

avatar image wyrrhcyn hctspheree · Mar 24, 2021 at 09:52 AM 0
Share

Could you share the whole code? I mean Unity and Java? To show everyone a clear answer for a future? :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

262 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges