Use InputConnection#setSelection to move cursor when supported

This commit is contained in:
Rocka 2025-11-18 22:42:37 +08:00
parent d934559d94
commit 9432a18398
No known key found for this signature in database
GPG Key ID: 28031158FFDD6853

View File

@ -229,8 +229,8 @@ class FcitxInputMethodService : LifecycleInputMethodService() {
when (it.sym.sym) {
FcitxKeyMapping.FcitxKey_BackSpace -> handleBackspaceKey()
FcitxKeyMapping.FcitxKey_Return -> handleReturnKey()
FcitxKeyMapping.FcitxKey_Left -> sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_LEFT)
FcitxKeyMapping.FcitxKey_Right -> sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT)
FcitxKeyMapping.FcitxKey_Left -> handleArrowKey(KeyEvent.KEYCODE_DPAD_LEFT)
FcitxKeyMapping.FcitxKey_Right -> handleArrowKey(KeyEvent.KEYCODE_DPAD_RIGHT)
else -> if (it.unicode > 0) {
commitText(Character.toString(it.unicode))
} else {
@ -388,6 +388,19 @@ class FcitxInputMethodService : LifecycleInputMethodService() {
}
}
private fun handleArrowKey(keyCode: Int) {
if (currentInputEditorInfo.inputType and InputType.TYPE_MASK_CLASS == InputType.TYPE_NULL) {
sendDownUpKeyEvents(keyCode)
return
}
val target = when (keyCode) {
KeyEvent.KEYCODE_DPAD_LEFT -> currentInputSelection.start - 1
KeyEvent.KEYCODE_DPAD_RIGHT -> currentInputSelection.end + 1
else -> return
}
currentInputConnection.setSelection(target, target)
}
fun commitText(text: String, cursor: Int = -1) {
val ic = currentInputConnection ?: return
// when composing text equals commit content, finish composing text as-is