title: Android : Catch mouse wheel button pressed/released before activity stops
body: <p>I'm developing an Android app in Java which must detect events from a real mouse connected in USB, and send them over the network to a computer which will use those events.</p>
  
  <p>My problem : I can detect mouse wheel button events (scroll, pressed, released), but when the user presses the wheel button, the app exits, and the callback is called afterwards.</p>
  
  <p>My question : is it possible to catch the event before the app exits, and prevent the default behavior ? If so, how ? Why do I catch the event too late ?</p>
  
  <p>Here is the functios declared in my activity :</p>
  
  <pre><code> @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);
  }
  </code></pre>
  
  <p>And here is how I prevented mouse right click from closing the app :
      public boolean onKeyUp(int keyCode, KeyEvent event) {
          int source = event.getSource();</p>
  
  <pre><code>    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;
          }
      } 
  }
  </code></pre>
  
  <p>Thanks for your help</p>