Android: Tracking mouse pointer movement
Asked Answered
M

3

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?

Mahla answered 28/3, 2011 at 3:45 Comment(0)
L
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.

Letishaletitia answered 28/8, 2012 at 2:37 Comment(2)
View.OnGenericMotion(View v, MotionEvent event) is available starting with api level 12: http://developer.android.com/reference/android/view/View.OnGenericMotionListener.html#onGenericMotionKagoshima
Hmm in my case i attached it to a webview and I only get events when I click on the left mouse button...Merwyn
A
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.

Acetal answered 28/3, 2011 at 4:1 Comment(5)
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?Mahla
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.Acetal
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?Mahla
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.Liselisetta
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..Galata
M
0

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

Merwyn answered 3/7, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.