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);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…