diff --git a/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/BaseKeyboard.kt b/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/BaseKeyboard.kt index 84a21447..d4b33d42 100644 --- a/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/BaseKeyboard.kt +++ b/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/BaseKeyboard.kt @@ -85,6 +85,9 @@ abstract class BaseKeyboard( */ private val touchTarget = hashMapOf() + private data class KeyActionState(val viewId: Int, val action: KeyAction) + private var keyActionStateBuffer: KeyActionState? = null + init { isMotionEventSplittingEnabled = true keyRows = keyLayout.map { row -> @@ -208,8 +211,27 @@ abstract class BaseKeyboard( def.behaviors.forEach { when (it) { is KeyDef.Behavior.Press -> { - setOnClickListener { _ -> - onAction(it.action) + keyPressListener = object : CustomGestureView.KeyPressListener { + override fun onKeyDown(view: CustomGestureView) { + keyActionStateBuffer?.let { state -> + onAction(state.action) + keyActionStateBuffer = null + } + keyActionStateBuffer = KeyActionState(view.id, it.action) + } + override fun onKeyUp(view: CustomGestureView) { + keyActionStateBuffer?.let { state -> + if (view.id == state.viewId) { + onAction(state.action) + keyActionStateBuffer = null + } + } + } + override fun onLongPress(view: CustomGestureView) { + if (view.id == keyActionStateBuffer?.viewId) { + keyActionStateBuffer = null + } + } } } is KeyDef.Behavior.LongPress -> { @@ -280,7 +302,10 @@ abstract class BaseKeyboard( is KeyDef.Popup.Keyboard -> { setOnLongClickListener { view -> view as KeyView - onPopupAction(PopupAction.ShowKeyboardAction(view.id, it, view.bounds)) + // only last key pressed will show the popup + if (view.id == keyActionStateBuffer?.viewId) { + onPopupAction(PopupAction.ShowKeyboardAction(view.id, it, view.bounds)) + } // do not consume this LongClick gesture false } diff --git a/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/CustomGestureView.kt b/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/CustomGestureView.kt index b7680446..60e14f92 100644 --- a/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/CustomGestureView.kt +++ b/app/src/main/java/org/fcitx/fcitx5/android/input/keyboard/CustomGestureView.kt @@ -23,6 +23,14 @@ import org.fcitx.fcitx5.android.input.keyboard.CustomGestureView.OnGestureListen open class CustomGestureView(ctx: Context) : FrameLayout(ctx) { + interface KeyPressListener { + fun onKeyDown(view: CustomGestureView) + fun onKeyUp(view: CustomGestureView) + fun onLongPress(view: CustomGestureView) + } + + var keyPressListener: KeyPressListener? = null + enum class SwipeAxis { X, Y } enum class GestureType { Down, Move, Up } @@ -177,6 +185,7 @@ open class CustomGestureView(ctx: Context) : FrameLayout(ctx) { swipeLastX = x swipeLastY = y } + keyPressListener?.onKeyDown(this) } MotionEvent.ACTION_UP -> { isPressed = false @@ -202,6 +211,10 @@ open class CustomGestureView(ctx: Context) : FrameLayout(ctx) { } else { performClick() } + keyPressListener?.onKeyUp(this) + } + if (longPressEnabled) { + keyPressListener?.onLongPress(this) } return true }