# onPreInterceptKeyBeforeSoftKeyboard

Common/AllAndroid/JVM

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/onPreInterceptKeyBeforeSoftKeyboard.modifier#(androidx.compose.ui.Modifier).onPreInterceptKeyBeforeSoftKeyboard(kotlin.Function1)( onPreInterceptKeyBeforeSoftKeyboard: (https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/KeyEvent) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Adding this `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` to the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard. | Cmn |

## Functions

### Modifier.onPreInterceptKeyBeforeSoftKeyboard

Cmn Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/input/key/SoftwareKeyboardInterceptionModifier.kt+function:onPreInterceptKeyBeforeSoftKeyboard) Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.5.0)

```
fun Modifier.onPreInterceptKeyBeforeSoftKeyboard(
    onPreInterceptKeyBeforeSoftKeyboard: (KeyEvent) -> Boolean
): Modifier
```

Adding this `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` to the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard. This can be used to intercept key input from a DPad, or physical keyboard connected to the device and is not applicable to input that is sent to the soft keyboard via spell check or autocomplete. This modifier is similar to `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/onInterceptKeyBeforeSoftKeyboard.modifier#(androidx.compose.ui.Modifier).onInterceptKeyBeforeSoftKeyboard(kotlin.Function1)`, but allows a parent composable to intercept the hardware key event before any child.

```kotlin
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.onPreviewKeyEvent

// When the inner Box is focused, and the user presses a key, the key goes down the hierarchy
// and then back up to the parent. At any stage you can stop the propagation by returning
// true to indicate that you consumed the event.
Box(Modifier.onPreviewKeyEvent { keyEvent1 -> false }.onKeyEvent { keyEvent4 -> false }) {
    Box(
        Modifier.onPreviewKeyEvent { keyEvent2 -> false }
            .onKeyEvent { keyEvent3 -> false }
            .focusable()
    )
}
```

| Parameters |
|---|---|
| `onPreInterceptKeyBeforeSoftKeyboard: (https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/KeyEvent) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | This callback is invoked when the user interacts with the hardware keyboard. It gives ancestors of a focused component the chance to intercept a `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/KeyEvent`. Return true to stop propagation of this event. If you return false, the key event will be sent to this `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/SoftKeyboardInterceptionModifierNode`'s child. If none of the children consume the event, it will be sent back up to the root `https://developer.android.com/reference/kotlin/androidx/compose/ui/input/key/KeyInputModifierNode` using the onKeyEvent callback, and ultimately to the software keyboard. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` | true if the event is consumed, false otherwise. |