Now, I am coding yet another app for Galaxy Tab (S6 Lite/S8+) with S Pen. To make this app minimum UI parts, I decided to get and test this Wacom One Standard Pen that has two buttons. I got this and I felt a little anxiety if android could not handle these buttons? But it was Okay. In this entry I will show you how to handle these buttons in Android View class.
As a result: this pen's 1st button has the same functionality S Pen button and 2nd button is as eraser. It makes sense.
In the extended class such as android.view.View various pen touch event called back to onTouchEvent function.
private companion object {
val ACTION_DOWN_WITH_1ST_BUTTON = 211
val ACTION_MOVE_WITH_1ST_BUTTON = 213
val ACTION_UP_WITH_1ST_BUTTON = 212
val ACTION_CANCEL_WITH_1ST_BUTTON = 214
}
override fun onTouchEvent(motionEvent: MotionEvent): Boolean {
val actionMasked = motionEvent.actionMasked
val eraser: Boolean = 0.until(motionEvent.pointerCount).filter { pointerIndex->
motionEvent.getToolType(pointerIndex)==MotionEvent.TOOL_TYPE_ERASER
}.size>0
if( !eraser ){
when(actionMasked){
//
// Pen Input without any buttons pressed
//
MotionEvent.ACTION_DOWN->{}
MotionEvent.ACTION_MOVE->{}
MotionEvent.ACTION_UP -> {}
MotionEvent.ACTION_CANCEL-> {}
//
// Pen Input with 1st buttons pressed ( SPen 1st button or Wacom One Standard Pen 1st button )
//
ACTION_DOWN_WITH_1ST_BUTTON->{}
ACTION_MOVE_WITH_1ST_BUTTON->{}
ACTION_UP_WITH_1ST_BUTTON -> {}
ACTION_CANCEL_WITH_1ST_BUTTON-> {}
//
else -> {}
}
} else {
when(actionMasked){
//
// Wacom One Standard Pen Input with 2nd button pressed
//
MotionEvent.ACTION_DOWN->{}
MotionEvent.ACTION_MOVE->{}
MotionEvent.ACTION_UP -> {}
MotionEvent.ACTION_CANCEL-> {}
//
else -> {}
}
}
return true
}
About 1st button:
Update: Galaxy tab S6Lite is Okay, but S8+ is NG. This code does not work properly on S8+. To capture this 1st button on S8+ use motionEvent.getButtonState() instead of using actionMasked value.
About 2nd button:
Another way to check which any button pressed, use motionEvent.getButtonState().
In details, see MotionEvent API.
You can check it this code:
when( motionEvent.getButtonState() ){
0 -> {
// no spen/wacom one button pressed
}
MotionEvent.BUTTON_STYLUS_PRIMARY -> {
// spen/wacom one 1st button pressed
}
MotionEvent.BUTTON_STYLUS_SECONDARY -< {
// NG: it is wrong. -> wacom one 2nd button pressed
}
else -> {
//
}
}
Update: Even if Wacom One Pen 2nd button pressed, it does not point motionEvent.getButtonState() value this MotionEvent.BUTTON_STYLUS_SECONDARY. To capture this 2nd button action, use motionEvent.getToolType instead of using motionEvent.getButtonState() value.
Wacom One Standard Pen works well on Galaxy Tab S6Lite/ S8+. If using these buttons, I could get rid of the pen/lasso/eraser tool switcher UI from the app.