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);
}
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.
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.
Switch to the Device Settings tab, click Settings. Check Scrolling and click Apply --> OK.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With