【问题标题】:Android : Catch mouse wheel button pressed/released before activity stopsAndroid:在活动停止之前按下/释放鼠标滚轮按钮
【发布时间】:2015-08-28 05:16:01
【问题描述】:

我正在用 Java 开发一个 Android 应用程序,它必须检测连接在 USB 中的真实鼠标的事件,并通过网络将它们发送到将使用这些事件的计算机。

我的问题:我可以检测到鼠标滚轮按钮事件(滚动、按下、释放),但是当用户按下滚轮按钮时,应用程序退出,之后会调用回调。

我的问题:是否可以在应用退出之前捕获事件并防止默认行为?如果是这样,如何?为什么我赶得太晚了?

这是我的活动中声明的功能:

 @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);
}

这就是我如何阻止鼠标右键单击关闭应用程序: public boolean onKeyUp(int keyCode, KeyEvent event) { int source = event.getSource();

    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;
        }
    } 
}

感谢您的帮助

【问题讨论】:

    标签: android events mousewheel


    【解决方案1】:

    如果您已处理该事件,则返回 true。不要向前传播。应用程序不会以这种方式关闭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多