Manage all mouse events in Android

Well, the question is rather simple - how can i handle left/right/middle click, wheel and (!)hover move in android 2/3/4.

I've been digging on this topic and found the following

  • in api 14 we can handle almost anything with fancy new MotionEvent
  • we also have Open Accessory that seems to be ok with USB mouse since api 12 (still missing bluetooth) (UPD OA backported to 2.3.4)
  • mouse actually works well on tablets with ICS and Honeycomb and cyanogen 2.3.7, but act like just single touch on every button.I wonder is there a way to intercept mouse messages.

Help me please to understand how i can handle bluetooth and usb mouse in most simple and right way in each version of android. Or say its impossible.

UPD2: seems like USB Host only in 3.1+, while USB Accessory useless for this task


These are my findings:

For Api Level < 9:

  • External mouse primary button is handled just as a normal finger touch. There seems to be no way to detect the mouse.
  • Secondary button is dispatched trough a KeyEvent with KeyEvent.KEYCODE_BACK. No way to distinguish between actual "Back" presses and secondary button presses.

For Api Level 9+:

  • A new method has been added MotionEvent.getSource(). I use this one to detect if input is from mouse.
  • Secondary button is still dispatched through a KeyEvent with KeyEvent.KEYCODE_BACK. On some devices the KeyEvent.getSource() returns InputDevice.SOURCE_MOUSE, so secondary button detection works in some cases.

For Api Level 12+:

  • OnGenericMotionListener has been added. I use this one to detect mouse moves with ACTION_HOVER_MOVE and wheel changes with ACTION_SCROLL.

For Api Level 14+:

  • New method MotionEvent.getButtonState(). I track this one to distinguish if a primary, secondary, tertiary mouse button is pressed when the MotionEvent.getActionMasked() is ACTION_MOVE, ACTION_DOWN or ACTION_UP.

I haven't looked into Api Level 15/16 or the tool type, because I'm able to track all mouse events with what I described above. Would be interesting if anybody has additional information or if I' missing out with 15/16/tooltypes.