8

Assuming that I am using a generic mouse, is it possible to track the X and Y coordinates of the mouse pointer in android?

asked Mar 28, 2011 at 3:45

3 Answers 3

10

You need a OnGenericMotionListener:

OnGenericMotion(...., MotionEvent me) {
if (me.getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE) {
}

api 14+ needed [confirmed] Found me a tablet with usb mouse and can confirm this works for mouse movement. You WILL get flooded with messages, so simple operations or sleeping should be considered.

answered Aug 28, 2012 at 2:37
Sign up to request clarification or add additional context in comments.

2 Comments

View.OnGenericMotion(View v, MotionEvent event) is available starting with api level 12: http://developer.android.com/reference/android/view/View.OnGenericMotionListener.html#onGenericMotion
Hmm in my case i attached it to a webview and I only get events when I click on the left mouse button...
0

The docs to ACTION_MOVE lead me to think that only drag events would be reported:

Constant for getAction(): A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). The motion contains the most recent point, as well as any intermediate points since the last down or move event.

answered Mar 28, 2011 at 4:01

5 Comments

Thank so much for your insight Matthew. I've also read about that. I think what I need to know if there is any way to track the mouse movement without having an ACTION_DOWN MotionEvent?
Do you have an Android device that has a mouse? If so you could test this out. I wasn't aware that there were any with mice.
Yeah, I actually have an android tablet that can use a mouse through USB port. Also, I already have a method using ACTION_DOWN, ACTION_MOVE, and ACTION_DOWN and yes, they are working fine. But is there any way to have an ACTION_MOVE event for a mouse pointer without having an ACTION_DOWN first?
Hi. I'm facing the same problem. Save you found a way to track the mouse movements. I heard that it is possible to get the mouse movements using Internal classes, but not easy.
does any one know how to use mouse from other system i.e mouse is connected to my laptop and laptop and tablet are connected using wi-fi direct.Now i want to use mouse in tablet..
0

In my case the OnGenericMotion solution did not work but attaching an OnHoverListener to the main view did the trick.

answered Jul 3, 2017 at 14:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.