keyboard: add haptics
- Commit
e2ccf8093ff6c71da3cfa3ca75890ec91d266f1d- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-29 19:10 UTC (2 months ago)
- Parents
3bea45c
Diff
keyboard/src/main/AndroidManifest.xml
diff --git a/keyboard/src/main/AndroidManifest.xml b/keyboard/src/main/AndroidManifest.xmlindex 4b2f86be1b..c95ae9cdee 100644--- a/keyboard/src/main/AndroidManifest.xml+++ b/keyboard/src/main/AndroidManifest.xml@@ -1,7 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> - <uses-permission android:name="android.permission.VIBRATE" /> <!-- Dictation (long-press on enter). Requested at that point by VoicePermissionActivity, never at startup: an IME cannot show a runtime permission dialog itself. --> <uses-permission android:name="android.permission.RECORD_AUDIO" />
keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.ktindex cf9ae852ed..ccb3a4fd04 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ime/KeyboardService.kt@@ -8,9 +8,6 @@ import android.media.AudioManager import android.net.Uri import android.os.Build import android.os.SystemClock-import android.os.VibrationEffect-import android.os.Vibrator-import android.os.VibratorManager import android.provider.Settings import android.text.InputType import android.view.KeyEvent@@ -163,8 +160,6 @@ class KeyboardService : InputMethodService(), private var editorActionId = EditorInfo.IME_ACTION_UNSPECIFIED private var enterSendsAction = false - private var vibrator: Vibrator? = null- private val voiceInput by lazy { VoiceInput(this) } /** Clears a dictation failure from the strip after a while; cancelled by the next change. */@@ -193,7 +188,6 @@ class KeyboardService : InputMethodService(), ds = DataStoreUtils.getInstance(this, deviceProtected = true) kbState.settings = KeyboardSettings.load(ds)- vibrator = getSystemService(VibratorManager::class.java)?.defaultVibrator // Load the dictionary off the main thread; suggestions stay empty until it is ready. scope.launch { dictionary = Dictionary.load(this@KeyboardService) }@@ -1250,17 +1244,13 @@ class KeyboardService : InputMethodService(), // --- Feedback --- /**- * A light key "tick" (like the stock keyboard), not a full-strength buzz. Built once:- * this runs before the edit on every single keypress, and both the effect and the- * service lookup were being redone each time.+ * Keypress sound only. Haptics moved to the press site in the key composables+ * ([rememberKeyHapticTick]): a service has no CompositionLocal, and ticking where the+ * press commits keeps keys, strips and pages from ever double-ticking one tap. */- private val keyTick by lazy { VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK) } private val audio by lazy { getSystemService(AudioManager::class.java) } private fun feedback() {- if (kbState.settings.haptic) {- vibrator?.vibrate(keyTick)- } if (kbState.settings.sound) { audio?.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD) }
keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardPage.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardPage.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardPage.ktindex 5874e4ea60..1a2eb260e9 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardPage.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardPage.kt@@ -85,6 +85,7 @@ fun ClipboardPage( @Composable private fun ClipRow(clip: ClipItem, onPaste: () -> Unit, onDelete: () -> Unit) {+ val tick = rememberKeyHapticTick() Row( modifier = Modifier .fillMaxWidth()@@ -97,7 +98,10 @@ private fun ClipRow(clip: ClipItem, onPaste: () -> Unit, onDelete: () -> Unit) { modifier = Modifier .weight(1f) .height(52.dp)- .clickable(onClick = onPaste)+ .clickable {+ tick()+ onPaste()+ } .padding(horizontal = 12.dp, vertical = 6.dp), verticalAlignment = Alignment.CenterVertically, ) {@@ -106,7 +110,10 @@ private fun ClipRow(clip: ClipItem, onPaste: () -> Unit, onDelete: () -> Unit) { Box( modifier = Modifier .height(52.dp)- .clickable(onClick = onDelete)+ .clickable {+ tick()+ onDelete()+ } .padding(horizontal = 12.dp), contentAlignment = Alignment.Center, ) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardStrip.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardStrip.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardStrip.ktindex 49bc611bb3..8328023e48 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardStrip.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/ClipboardStrip.kt@@ -61,6 +61,7 @@ fun ClipboardStrip( onPaste: () -> Unit, onDelete: () -> Unit, ) {+ val tick = rememberKeyHapticTick() Row( modifier = Modifier .fillMaxWidth()@@ -72,7 +73,10 @@ fun ClipboardStrip( modifier = Modifier .fillMaxHeight() .clip(RoundedCornerShape(8.dp))- .clickable(onClick = onOpen)+ .clickable {+ tick()+ onOpen()+ } .padding(horizontal = 6.dp), contentAlignment = Alignment.Center, ) {@@ -87,7 +91,10 @@ fun ClipboardStrip( .fillMaxHeight() .clip(RoundedCornerShape(8.dp)) .background(MaterialTheme.colorScheme.surfaceContainerHigh)- .clickable(onClick = onPaste)+ .clickable {+ tick()+ onPaste()+ } .padding(horizontal = 10.dp), verticalAlignment = Alignment.CenterVertically, ) {@@ -97,7 +104,10 @@ fun ClipboardStrip( modifier = Modifier .fillMaxHeight() .clip(RoundedCornerShape(8.dp))- .clickable(onClick = onDelete)+ .clickable {+ tick()+ onDelete()+ } .padding(horizontal = 6.dp), contentAlignment = Alignment.Center, ) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiPage.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiPage.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiPage.ktindex aa80c0d536..9deb9637a1 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiPage.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiPage.kt@@ -58,6 +58,9 @@ fun EmojiPage( // Not keyed on `recents`, so picking an emoji never yanks the tab out from under you. var category by remember(data) { mutableIntStateOf(if (recents.isEmpty()) 1 else 0) } val selected = categories.getOrNull(category) ?: categories.first()+ // Ticks for grid commits and tab switches below โ captured here since LazyVerticalGrid+ // item content can't read CompositionLocals through its own remember scope cleanly.+ val tick = rememberKeyHapticTick() if (selected.emojis.isEmpty()) { Box( modifier = Modifier@@ -82,7 +85,10 @@ fun EmojiPage( Box( modifier = Modifier .aspectRatio(1f)- .clickable { onEmoji(emoji) },+ .clickable {+ tick()+ onEmoji(emoji)+ }, contentAlignment = Alignment.Center, ) { Text(text = emoji, fontSize = 24.sp)@@ -102,7 +108,10 @@ fun EmojiPage( CategoryTab( label = cat.label, selected = index == category,- onClick = { category = index },+ onClick = {+ tick()+ category = index+ }, ) } RepeatKey(height = keyHeight, weight = 1.3f, onRepeat = onBackspace) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiSearch.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiSearch.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiSearch.ktindex 0907dce8e7..66d25f1a91 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiSearch.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/EmojiSearch.kt@@ -35,6 +35,7 @@ import com.vayunmathur.library.ui.Text */ @Composable fun EmojiSearchBar(height: Dp, query: String, onClose: () -> Unit) {+ val tick = rememberKeyHapticTick() Row( modifier = Modifier .fillMaxWidth()@@ -63,7 +64,10 @@ fun EmojiSearchBar(height: Dp, query: String, onClose: () -> Unit) { Box( modifier = Modifier .fillMaxHeight()- .clickable(onClick = onClose)+ .clickable {+ tick()+ onClose()+ } .padding(horizontal = 6.dp), contentAlignment = Alignment.Center, ) {@@ -78,6 +82,7 @@ fun EmojiSearchBar(height: Dp, query: String, onClose: () -> Unit) { /** The matches for the current query, scrolling sideways. Tapping one commits it. */ @Composable fun EmojiSearchResults(height: Dp, results: List<String>, onPick: (String) -> Unit) {+ val tick = rememberKeyHapticTick() LazyRow( modifier = Modifier .fillMaxWidth()@@ -88,7 +93,10 @@ fun EmojiSearchResults(height: Dp, results: List<String>, onPick: (String) -> Un Box( modifier = Modifier .fillMaxHeight()- .clickable { onPick(emoji) }+ .clickable {+ tick()+ onPick(emoji)+ } .padding(horizontal = 10.dp), contentAlignment = Alignment.Center, ) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/FunctionKeys.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/FunctionKeys.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/FunctionKeys.ktindex 26ad4d8b48..0347c07c31 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/FunctionKeys.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/FunctionKeys.kt@@ -56,6 +56,7 @@ fun RowScope.SpecialKey( ) { val interaction = remember { MutableInteractionSource() } val pressed by interaction.collectIsPressedAsState()+ val tick = rememberKeyHapticTick() Box( modifier = Modifier .weight(weight)@@ -64,8 +65,14 @@ fun RowScope.SpecialKey( interactionSource = interaction, indication = null, onLongClickLabel = onLongClickLabel,- onLongClick = onLongClick,- onClick = onClick,+ onLongClick = {+ tick()+ onLongClick?.invoke()+ },+ onClick = {+ tick()+ onClick()+ }, ) .padding(KeyPadding) .clip(KeyShape)@@ -91,6 +98,7 @@ fun RowScope.SpaceKey( ) { val interaction = remember { MutableInteractionSource() } val pressed by interaction.collectIsPressedAsState()+ val tick = rememberKeyHapticTick() Box( modifier = Modifier .weight(weight)@@ -98,7 +106,10 @@ fun RowScope.SpaceKey( .clickable( interactionSource = interaction, indication = null,- onClick = onSpace,+ onClick = {+ tick()+ onSpace()+ }, ) .padding(KeyPadding) .clip(KeyShape)@@ -126,6 +137,8 @@ fun RowScope.RepeatKey( val interaction = remember { MutableInteractionSource() } val pressed by interaction.collectIsPressedAsState() val scope = androidx.compose.runtime.rememberCoroutineScope()+ // Captured for the pointerInput block below, which can't read CompositionLocals itself.+ val currentTick by androidx.compose.runtime.rememberUpdatedState(rememberKeyHapticTick()) Box( modifier = Modifier .weight(weight)@@ -134,6 +147,9 @@ fun RowScope.RepeatKey( detectTapGestures(onPress = { offset -> val press = PressInteraction.Press(offset) interaction.emit(press)+ // Once per press, not per repeat: holding backspace buzzes once, then+ // deletes silently the way Gboard does.+ currentTick() onRepeat() val job = scope.launch { delay(350)@@ -179,6 +195,8 @@ fun RowScope.ShiftKey( active -> MaterialTheme.colorScheme.onPrimaryContainer else -> MaterialTheme.colorScheme.onSurface }+ // Captured for the pointerInput block below, which can't read CompositionLocals itself.+ val currentTick by androidx.compose.runtime.rememberUpdatedState(rememberKeyHapticTick()) Box( modifier = Modifier .weight(weight)@@ -194,7 +212,10 @@ fun RowScope.ShiftKey( interaction.emit( if (released) PressInteraction.Release(press) else PressInteraction.Cancel(press), )- if (released) onShift()+ if (released) {+ currentTick()+ onShift()+ } }, ) }
keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyButton.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyButton.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyButton.ktindex 0974b73500..6b3b847fbb 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyButton.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyButton.kt@@ -122,6 +122,10 @@ fun RowScope.CharKey( val full = with(density) { AlternateWidth.toPx() } if (options.isEmpty()) full else minOf(full, screenWidth / options.length) }+ // Key tick at each commit site below โ the gesture runs in pointerInput where+ // LocalHapticFeedback can't be read, so it comes through this captured lambda.+ val tick = rememberKeyHapticTick()+ val currentTick by rememberUpdatedState(tick) // The gesture below reads all of these through snapshots rather than capturing them, so it // never has to be restarted to pick up a new value. See the note on `pointerInput(Unit)`.@@ -187,7 +191,10 @@ fun RowScope.CharKey( if (!change.pressed) up = change } if (up != null) {- if (up.position.isInside(size)) currentOnClick()+ if (up.position.isInside(size)) {+ currentTick()+ currentOnClick()+ } return@awaitEachGesture } val opts = currentOptions@@ -195,6 +202,7 @@ fun RowScope.CharKey( if (opts.isEmpty() || onAlt == null) { // A hold with nothing to offer still types the key; the old // code waited for the lift and then committed nothing.+ currentTick() currentOnClick() return@awaitEachGesture }@@ -214,6 +222,7 @@ fun RowScope.CharKey( continue } if (!change.pressed) {+ currentTick() onAlt(opts[picked].toString()) break }
keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyHaptics.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyHaptics.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyHaptics.ktnew file mode 100644index 0000000000..0902812282--- /dev/null+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyHaptics.kt@@ -0,0 +1,30 @@+package com.vayunmathur.keyboard.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.compositionLocalOf+import androidx.compose.runtime.remember+import com.vayunmathur.library.ui.rememberHaptics++/**+ * Whether key ticks are enabled, from the keyboard's haptic setting (default on, toggled in+ * SetupScreen). Provided once in [KeyboardScreen]; every key and tappable strip reads it here+ * so the toggle needs no plumbing through the dozen components below.+ *+ * The shared [rememberHaptics] already respects the *system* haptic setting, so a tick fires+ * only when both the app toggle and the system setting allow it.+ */+val LocalKeyHapticsEnabled = compositionLocalOf { true }++/**+ * A stable key-tick lambda for gesture and click callbacks. Read once during composition โ+ * haptics can't be read from inside a `pointerInput` block or a stored callback, so call+ * sites capture this and invoke it where the press commits.+ */+@Composable+fun rememberKeyHapticTick(): () -> Unit {+ val haptics = rememberHaptics()+ val enabled = LocalKeyHapticsEnabled.current+ return remember(haptics, enabled) {+ { if (enabled) haptics.keyPress() }+ }+}
keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyboardScreen.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyboardScreen.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyboardScreen.ktindex 971546c644..e81c6f8824 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyboardScreen.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/KeyboardScreen.kt@@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable+import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.remember@@ -54,6 +55,9 @@ fun KeyboardScreen(state: KeyboardState, actions: ImeActions) { // and only arriving at or leaving search should rearrange the keyboard. val searching by remember(state) { derivedStateOf { state.emojiQuery != null } } val voice = state.voice+ // Key ticks are gated by the app's haptic setting; LocalHapticFeedback adds the system+ // setting on top. Provided here so no key or strip component needs the flag threaded in.+ CompositionLocalProvider(LocalKeyHapticsEnabled provides state.settings.haptic) { Surface(color = MaterialTheme.colorScheme.surfaceContainer) { Column( modifier = Modifier@@ -87,6 +91,7 @@ fun KeyboardScreen(state: KeyboardState, actions: ImeActions) { } } }+ } } /** The emoji-search query bar and its results, which change together on every keystroke. */@@ -160,11 +165,15 @@ private fun Strip(state: KeyboardState, actions: ImeActions) { // space bar and its neighbours back a key's width, and this row is present on the // symbol pages too, so the clipboard does not vanish behind ?123. if (clipboard) {+ val stripTick = rememberKeyHapticTick() Box( modifier = Modifier .fillMaxHeight() .clip(RoundedCornerShape(8.dp))- .clickable { actions.setPage(KeyboardPage.CLIPBOARD) }+ .clickable {+ stripTick()+ actions.setPage(KeyboardPage.CLIPBOARD)+ } .padding(horizontal = 12.dp), contentAlignment = Alignment.Center, ) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/SuggestionStrip.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SuggestionStrip.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SuggestionStrip.ktindex 8284c20b61..b365746438 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SuggestionStrip.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/SuggestionStrip.kt@@ -34,6 +34,7 @@ fun CandidateStrip( candidates: List<String>, onPick: (String) -> Unit, ) {+ val tick = rememberKeyHapticTick() LazyRow( modifier = Modifier .fillMaxWidth()@@ -51,7 +52,10 @@ fun CandidateStrip( Box( modifier = Modifier .fillMaxHeight()- .clickable { onPick(candidate) }+ .clickable {+ tick()+ onPick(candidate)+ } .padding(horizontal = 14.dp), contentAlignment = Alignment.Center, ) {@@ -82,6 +86,7 @@ fun SuggestionStrip( suggestions: List<String>, onPick: (String) -> Unit, ) {+ val tick = rememberKeyHapticTick() Row( modifier = Modifier .fillMaxWidth()@@ -104,7 +109,10 @@ fun SuggestionStrip( modifier = Modifier .weight(1f) .fillMaxHeight()- .clickable { onPick(word) }+ .clickable {+ tick()+ onPick(word)+ } .padding(horizontal = 8.dp), contentAlignment = Alignment.Center, ) {
keyboard/src/main/java/com/vayunmathur/keyboard/ui/VoiceStrip.kt
diff --git a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/VoiceStrip.kt b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/VoiceStrip.ktindex 8a758126c8..86d32639d6 100644--- a/keyboard/src/main/java/com/vayunmathur/keyboard/ui/VoiceStrip.kt+++ b/keyboard/src/main/java/com/vayunmathur/keyboard/ui/VoiceStrip.kt@@ -99,11 +99,15 @@ fun VoiceStrip( @Composable private fun StripButton(onClick: () -> Unit, content: @Composable () -> Unit) {+ val tick = rememberKeyHapticTick() Box( modifier = Modifier .fillMaxHeight() .clip(RoundedCornerShape(8.dp))- .clickable(onClick = onClick)+ .clickable {+ tick()+ onClick()+ } .padding(horizontal = 8.dp), contentAlignment = Alignment.Center, ) { content() }
library/ui/src/main/java/com/vayunmathur/library/ui/Haptics.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/Haptics.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/Haptics.ktindex f3cc90a414..df2aee5984 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/Haptics.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/Haptics.kt@@ -23,6 +23,9 @@ class Haptics internal constructor(private val feedback: HapticFeedback) { /** Something has been picked up: a long press has taken the gesture. */ fun longPress() = feedback.performHapticFeedback(HapticFeedbackType.LongPress) + /** A virtual key was pressed: a keyboard or keypad key committed. */+ fun keyPress() = feedback.performHapticFeedback(HapticFeedbackType.KeyboardTap)+ /** * A boundary was crossed - a new drop target, a slider notch. The lightest thing available, * because this fires repeatedly while the finger is moving.