如何区分鼠标事件和触摸屏事件?

时间:2019-08-08 20:59:24

标签: java android input mouse touchscreen

在我的应用程序中,我需要知道哪个输入设备产生了触摸事件:鼠标,触摸屏,触摸板或其他东西。

event.getSource()返回:

~/.bash_profile

我已经制作了一种输出到logcat类型的源的方法:

for mouse: 8194
for touchscreen: 4098

但是它为鼠标和触摸屏输出void dumpSource(MotionEvent e) { int s = e.getSource(); Log.e("LorieService", "Motion event is from sources: " + ((s&InputDevice.SOURCE_KEYBOARD)!=0?"keyboard ":"") + ((s&InputDevice.SOURCE_DPAD)!=0?"dpad ":"") + ((s&InputDevice.SOURCE_GAMEPAD)!=0?"gamepad ":"") + ((s&InputDevice.SOURCE_TOUCHSCREEN)!=0?"touchscreen ":"") + ((s&InputDevice.SOURCE_MOUSE)!=0?"mouse ":"") + ((s&InputDevice.SOURCE_STYLUS)!=0?"stylus ":"") + ((s&InputDevice.SOURCE_BLUETOOTH_STYLUS)!=0?"bt_stylus ":"") + ((s&InputDevice.SOURCE_TRACKBALL)!=0?"trackball ":"") + ((s&InputDevice.SOURCE_MOUSE_RELATIVE)!=0?"mouse_relative ":"") + ((s&InputDevice.SOURCE_TOUCHPAD)!=0?"touchpad ":"") + ((s&InputDevice.SOURCE_TOUCH_NAVIGATION)!=0?"touch_navigation ":"") + ((s&InputDevice.SOURCE_ROTARY_ENCODER)!=0?"rotary_encoder ":"") + ((s&InputDevice.SOURCE_JOYSTICK)!=0?"joystick ":"") + ((s&InputDevice.SOURCE_HDMI)!=0?"hdmi":"") ); }

如何正确区分鼠标和触摸屏事件?

1 个答案:

答案 0 :(得分:1)

这不是检查方法。正确的检查方法是type = s&InputDevice.SOURCE_MASK;,然后检查类型是否相等。如果两种设备类型的源中的任何位相同,则执行方法将返回true。该类型本身不是位掩码,而是一个整数枚举。