Skip to content

Commit 7f29b8b

Browse files
authored
fix: perform haptic feedback on cursor move (#228)
* fix: perform haptic feedback on cursor control This uses `HapticFeedbackConstants.TEXT_HANDLE_MOVE`, which may not work on all devices. See: #222 * fix: bump `HANDLE_MOVE_MIN_MS` to 35ms Less buzzing when swiping quickly on some devices.
1 parent 4a1e582 commit 7f29b8b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

app/src/main/kotlin/org/fossify/keyboard/services/SimpleKeyboardIME.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,20 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared
417417
}
418418

419419
private fun moveCursor(moveRight: Boolean) {
420-
val extractedText = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0) ?: return
421-
var newCursorPosition = extractedText.selectionStart
422-
newCursorPosition = if (moveRight) {
423-
newCursorPosition + 1
420+
val inputConnection = currentInputConnection
421+
val extractedText = inputConnection.getExtractedText(ExtractedTextRequest(), 0) ?: return
422+
val text = extractedText.text ?: return
423+
val oldPos = extractedText.selectionStart
424+
val newPos = if (moveRight) {
425+
oldPos + 1
424426
} else {
425-
newCursorPosition - 1
426-
}
427+
oldPos - 1
428+
}.coerceIn(0, text.length)
427429

428-
currentInputConnection?.setSelection(newCursorPosition, newCursorPosition)
430+
if (newPos != oldPos) {
431+
inputConnection?.setSelection(newPos, newPos)
432+
keyboardView?.performHapticHandleMove()
433+
}
429434
}
430435

431436
private fun getImeOptionsActionId(): Int {

app/src/main/kotlin/org/fossify/keyboard/views/MyKeyboardView.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.os.Build
1414
import android.os.Handler
1515
import android.os.Looper
1616
import android.os.Message
17+
import android.os.SystemClock
1718
import android.util.AttributeSet
1819
import android.util.TypedValue
1920
import android.view.*
@@ -33,6 +34,7 @@ import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED
3334
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
3435
import org.fossify.commons.extensions.*
3536
import org.fossify.commons.helpers.ensureBackgroundThread
37+
import org.fossify.commons.helpers.isOreoMr1Plus
3638
import org.fossify.commons.helpers.isPiePlus
3739
import org.fossify.keyboard.R
3840
import org.fossify.keyboard.activities.ManageClipboardItemsActivity
@@ -143,6 +145,7 @@ class MyKeyboardView @JvmOverloads constructor(
143145
private var mTopSmallNumberMarginHeight = 0f
144146
private val mSpaceMoveThreshold: Int
145147
private var ignoreTouches = false
148+
private var lastHandleMoveAt = 0L
146149

147150
private var mKeyBackground: Drawable? = null
148151
private var mShowKeyBorders: Boolean = false
@@ -185,6 +188,7 @@ class MyKeyboardView @JvmOverloads constructor(
185188
private const val REPEAT_INTERVAL = 50 // ~20 keys per second
186189
private const val REPEAT_START_DELAY = 400
187190
private val LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout()
191+
private const val HANDLE_MOVE_MIN_MS = 35L
188192
}
189193

190194
init {
@@ -491,6 +495,20 @@ class MyKeyboardView @JvmOverloads constructor(
491495
}
492496
}
493497

498+
fun performHapticHandleMove() {
499+
if (!context.config.vibrateOnKeypress) return
500+
val now = SystemClock.uptimeMillis()
501+
if (now - lastHandleMoveAt < HANDLE_MOVE_MIN_MS) return
502+
lastHandleMoveAt = now
503+
if (isOreoMr1Plus()) {
504+
@Suppress("DEPRECATION")
505+
performHapticFeedback(
506+
HapticFeedbackConstants.TEXT_HANDLE_MOVE,
507+
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING
508+
)
509+
}
510+
}
511+
494512
/**
495513
* Sets the state of the shift key of the keyboard, if any.
496514
* @param shifted whether or not to enable the state of the shift key

0 commit comments

Comments
 (0)