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?

John Erick
  • 83
  • 1
  • 5

3 Answers3

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.

Janus Kjempff
  • 141
  • 1
  • 4
  • `View.OnGenericMotion(View v, MotionEvent event)` is available starting with api level 12: [http://developer.android.com/reference/android/view/View.OnGenericMotionListener.html#onGenericMotion](http://developer.android.com/reference/android/view/View.OnGenericMotionListener.html#onGenericMotion) – Bianca Daniciuc Aug 06 '13 at 12:38
  • 1
    Hmm in my case i attached it to a webview and I only get events when I click on the left mouse button... – Roel Jul 03 '17 at 14:04
0

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

Roel
  • 3,089
  • 2
  • 30
  • 34
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.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • 1
    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? – John Erick Mar 28 '11 at 04:15
  • 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. – Matthew Mar 28 '11 at 04:35
  • 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? – John Erick Mar 28 '11 at 04:54
  • 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. – SAN Mar 06 '13 at 22:11
  • 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.. – Srb May 09 '14 at 09:19