From 6865a7ffc56401795e25919190130b9ecd8741ef Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Sun, 22 Nov 2026 13:55:19 +0530 Subject: [PATCH] fix(cursor): stop space swipe at the edge of the text (fixes #891) Space swipe sends one arrow key per step. At the edge of the text the app moves the focus to another field instead, so the cursor jumped out of the field. The swipe now stops at the start and end of the text, for horizontal and vertical swipes. The text before the cursor is asked from the editor, as arrow keys don't update the cached text and cursor position. --- .../keyboard/keyboard/KeyboardActionListenerImpl.kt | 13 ++++++++++++- .../helium314/keyboard/latin/RichInputConnection.kt | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardActionListenerImpl.kt b/app/src/main/java/helium314/keyboard/keyboard/KeyboardActionListenerImpl.kt index 1d06149c82..e6d45bd2ea 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardActionListenerImpl.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardActionListenerImpl.kt @@ -1031,6 +1031,9 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp KeyCode.ARROW_DOWN } repeat(abs(steps)) { + // up at the start or down at the end of the text moves the focus to another field (#891) + val next = if (steps < 0) connection.getTextBeforeCursorFromEditor(1) else connection.getTextAfterCursor(1, 0) + if (next != null && next.isEmpty()) return true onCodeInput(code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false) } return true @@ -1058,6 +1061,14 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp inputLogic.finishInput() } + // An arrow key at the edge of the text moves the focus to another field (#891), so stop at the edge. + // Steps < 0 move back in the text (also for RTL). Without an answer from the editor, move as before. + val available = if (steps < 0) connection.getTextBeforeCursorFromEditor(abs(steps)) + else connection.getTextAfterCursor(abs(steps), 0) + val moveCount = if (available == null) abs(steps) + else minOf(abs(steps), Character.codePointCount(available, 0, available.length)) + if (moveCount == 0) return true + cursorMoveStepCount++ val shouldHaptic = cursorMoveStepCount % 2 != 0 val code = if (steps < 0) { @@ -1067,7 +1078,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp if (shouldHaptic) gestureMoveForwardHaptics(true) if (rtl) KeyCode.ARROW_LEFT else KeyCode.ARROW_RIGHT } - repeat(abs(steps)) { + repeat(moveCount) { onCodeInput(code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false) } return true diff --git a/app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt b/app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt index 12e34308b1..ca9a74a6d8 100644 --- a/app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt +++ b/app/src/main/java/helium314/keyboard/latin/RichInputConnection.kt @@ -529,6 +529,17 @@ class RichInputConnection(private val mParent: InputMethodService) : PrivateComm return !TextUtils.isEmpty(after) } + /** Text before the cursor asked from the editor, not from the cache: arrow key events don't update the cache */ + fun getTextBeforeCursorFromEditor(n: Int): CharSequence? { + if (n <= 0 || !isConnected()) return null + return try { + mIC?.getTextBeforeCursor(n, 0) + } catch (e: Exception) { + Log.w(TAG, "getTextBeforeCursor threw in host app", e) + null + } + } + fun hasComposingText(): Boolean = mComposingText.isNotEmpty() fun getCursorCapsMode(