Android:在活动停止前按下/释放鼠标滚轮按钮
我正在用Java开发一个Android应用程序,它必须检测USB连接的真实鼠标的事件,并通过网络将它们发送到使用这些事件的计算机 我的问题:我可以检测鼠标滚轮按钮事件(滚动、按下、释放),但当用户按下滚轮按钮时,应用程序退出,然后调用回调 我的问题:是否有可能在应用程序退出之前捕获事件,并阻止默认行为?如果是,怎么做?为什么我来不及赶上这个活动 以下是在我的活动中声明的函数:Android:在活动停止前按下/释放鼠标滚轮按钮,android,events,mousewheel,Android,Events,Mousewheel,我正在用Java开发一个Android应用程序,它必须检测USB连接的真实鼠标的事件,并通过网络将它们发送到使用这些事件的计算机 我的问题:我可以检测鼠标滚轮按钮事件(滚动、按下、释放),但当用户按下滚轮按钮时,应用程序退出,然后调用回调 我的问题:是否有可能在应用程序退出之前捕获事件,并阻止默认行为?如果是,怎么做?为什么我来不及赶上这个活动 以下是在我的活动中声明的函数: @Override public boolean onGenericMotionEvent(MotionEvent
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
int pointerId = event.getPointerId(0);
if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
Log.d(name, "onGenericMotionEvent : MotionEvent.ACTION_HOVER_MOVE " + MotionEvent.ACTION_HOVER_MOVE);
return true;
} else if (event.getAction() == MotionEvent.ACTION_SCROLL) {
Log.d(name, "onGenericMotionEvent : MotionEvent.ACTION_SCROLL " + MotionEvent.ACTION_SCROLL);
return true;
} else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
Log.d(name, "why does is happen after onPause ??? onGenericMotionEvent : MotionEvent.ACTION_HOVER_EXIT " + MotionEvent.ACTION_HOVER_EXIT);
return true;
} else {
//Log.d(name, "onGenericMotionEvent : " + MotionEvent.actionToString(event.getAction()) + " " + event.getAction());
}
return super.onGenericMotionEvent(event);
}
下面是我如何阻止鼠标右键单击关闭应用程序的:
公共布尔onKeyUp(int-keyCode,KeyEvent事件){
int source=event.getSource()
感谢您的帮助如果您已处理该事件,请返回true。不要将其转发。应用程序将不会以这种方式关闭
boolean mouseRightButton = false;
if (source == InputDevice.SOURCE_TOUCHSCREEN) {
Log.e(name, "onKeyUp from touchscreen");
} else if (source == InputDevice.SOURCE_MOUSE) {
Log.e(name, "onKeyUp from mouse");
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e(name, "right click");
mouseRightButton = true;
return true;
}
}
}