Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my view respond to a mousewheel?

Tags:

android

mouse

I have a view with an onTouch that can distinguish between touch input and left/middle/right mouse clicks, as in

@Override
public boolean onTouch(View v, MotionEvent event) {
  if (event.getButtonState() == MotionEvent.BUTTON_PRIMARY) // API 14 required
    ...
  ...
}

I'd also like this view to respond to the mousewheel. onTouch is not the way, nor have I found any other event handler to respond to the mousewheel. Maybe the view can pretend to be scrollable and do its own thing with scrolling methods? At this point, I've given up, and am using keyboard input (1 through 9, plus 0) to select displayed elements that I'd prefer to select with the mousewheel.
So a firm hint or a bit of code would be appreciated.

Don't worry that an Android UI requiring a keyboard and mouse will be inflicted on the public; the app is a development tool.

EDIT: the correct answer is given below, but just so this question is more helpful to future readers, this is (slightly edited) the actual code that I'm using as a result:

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
  if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_SCROLL:
        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
          selectNext()
        else
          selectPrev();
        return true;
    }
  }
  return super.onGenericMotionEvent(event);
}
like image 238
Julian Fondren Avatar asked Jun 13 '12 23:06

Julian Fondren


People also ask

How does a Mousewheel work?

Each wheel is made up of plastic spokes and, as it turns, the spokes repeatedly break a light beam. The more the wheel turns, the more times the beam is broken. So counting the number of times the beam is broken is a way of precisely measuring how far the wheel has turned and how far you've pushed the mouse.

How do you bind Mousewheel to click?

Go to the normal mouse tab, add a new button, go to the "click here to select mouse button" area and scroll the wheel. It will capture that action and you may assign it to what you want. Save this answer.

How do I enable scroll click?

Switch to the Device Settings tab, click Settings. Check Scrolling and click Apply --> OK.


1 Answers

The accepted answer was a document link that led me to the example code in the question, but that answer was deleted. So that this question no longer appears 'unanswered', this is how your view can respond to a mousewheel:

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
  if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_SCROLL:
        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
          selectNext();
        else
          selectPrev();
        return true;
    }
  }
  return super.onGenericMotionEvent(event);
}
like image 78
Julian Fondren Avatar answered Oct 13 '22 00:10

Julian Fondren