Handle all mouse events in Android

后端 未结 2 846
感动是毒
感动是毒 2020-12-24 04:31

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

相关标签:
2条回答
  • 2020-12-24 04:57

    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.

    0 讨论(0)
  • 2020-12-24 05:01

    OK. I think i got a clue. I've read highlights about android 3.x/4.x and realized that...

    1. we can use mouse in android 3+, in older versions mouse is device-specific

    2. we can intercept recognized mouse events from any source (BT or USB)...

    2.1. ... scroll or hover_move in 3.1+ in onGenericMotionEvent

    2.2. ... primary, secondary, and tertiary buttons and hover enter/leave in android 4.0+ (and finally TOOL_TYPE_MOUSE constant)

    The other option is to parse pretty lowlevel data from bluetooth socket or usb in host mode.

    Am i right? Still wonder if anybody have better solution.

    0 讨论(0)
提交回复
热议问题