处理Android中的所有鼠标事件 [英] Handle all mouse events in Android
问题描述
好吧,问题很简单 - 我如何处理 android 2/3/4 中的左/右/中点击、滚轮和(!)悬停移动.
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
- 在 api 14 中,我们几乎可以用新奇的 MotionEvent 处理任何事情
- 从 api 12(仍然缺少蓝牙)(UPD OA 向后移植到 2.3.4)以来,我们还有 Open Accessory 似乎可以与 USB 鼠标配合使用
- 鼠标实际上在具有 ICS 和 Honeycomb 和 cyanogen 2.3.7 的平板电脑上运行良好,但就像在每个按钮上单击一下一样.我想知道有没有办法拦截鼠标消息.
请帮助我了解如何在每个版本的 android 中以最简单和正确的方式处理蓝牙和 USB 鼠标.或者说这是不可能的.
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:似乎只有 3.1+ 的 USB Host,而 USB Accessory 对这个任务没用
UPD2: seems like USB Host only in 3.1+, while USB Accessory useless for this task
推荐答案
这些是我的发现:
对于 API 级别 <9:
For Api Level < 9:
- 外部鼠标主按钮的处理方式与正常的手指触摸一样.似乎没有办法检测到鼠标.
- 辅助按钮通过带有
KeyEvent.KEYCODE_BACK
的KeyEvent
分派.无法区分实际的返回"按下和辅助按钮按下.
- 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
withKeyEvent.KEYCODE_BACK
. No way to distinguish between actual "Back" presses and secondary button presses.
对于 API 级别 9+:
For Api Level 9+:
- 添加了一个新方法
MotionEvent.getSource()
.我用这个来检测输入是否来自鼠标. - 次要按钮仍通过带有
KeyEvent.KEYCODE_BACK
的KeyEvent
调度.在某些设备上,KeyEvent.getSource()
返回InputDevice.SOURCE_MOUSE
,因此辅助按钮检测在某些情况下有效.
- 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
withKeyEvent.KEYCODE_BACK
. On some devices theKeyEvent.getSource()
returnsInputDevice.SOURCE_MOUSE
, so secondary button detection works in some cases.
对于 API 级别 12+:
For Api Level 12+:
OnGenericMotionListener
已添加.我用这个来检测ACTION_HOVER_MOVE
的鼠标移动和ACTION_SCROLL
的滚轮变化.
OnGenericMotionListener
has been added. I use this one to detect mouse moves withACTION_HOVER_MOVE
and wheel changes withACTION_SCROLL
.
对于 API 级别 14+:
For Api Level 14+:
- 新方法
MotionEvent.getButtonState()
.当MotionEvent.getActionMasked()
为ACTION_MOVE
、ACTION_DOWN
或ACTION_UP
.
- New method
MotionEvent.getButtonState()
. I track this one to distinguish if a primary, secondary, tertiary mouse button is pressed when theMotionEvent.getActionMasked()
isACTION_MOVE
,ACTION_DOWN
orACTION_UP
.
我没有研究过 Api 级别 15/16 或工具类型,因为我能够使用上述内容跟踪所有鼠标事件.如果有人有其他信息或者我错过了 15/16/tooltypes,那将会很有趣.
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.
这篇关于处理Android中的所有鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!