# InterceptPlatformTextInput

Common/AllAndroid/JVM

## Functions summary

|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/InterceptPlatformTextInput.composable#InterceptPlatformTextInput(androidx.compose.ui.platform.PlatformTextInputInterceptor,kotlin.Function0)( interceptor: https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputInterceptor, content: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Intercept all calls to `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputSession#startInputMethod(androidx.compose.ui.platform.PlatformTextInputMethodRequest)` from below where this composition local is provided with the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputInterceptor`. | Cmn |

## Functions

### InterceptPlatformTextInput

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/platform/PlatformTextInputModifierNode.kt+function:InterceptPlatformTextInput) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.7.0)

```
@Composable
fun InterceptPlatformTextInput(
    interceptor: PlatformTextInputInterceptor,
    content: @Composable () -> Unit
): Unit
```

Intercept all calls to `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputSession#startInputMethod(androidx.compose.ui.platform.PlatformTextInputMethodRequest)` from below where this composition local is provided with the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputInterceptor`.

If a different interceptor instance is passed between compositions while a text input session is active, the upstream session will be torn down and restarted with the new interceptor. The downstream session (i.e. the call to `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/PlatformTextInputSession#startInputMethod(androidx.compose.ui.platform.PlatformTextInputMethodRequest)`) will *not* be cancelled and the request will be re-used to pass to the new interceptor.

```kotlin
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.ui.platform.InterceptPlatformTextInput
import androidx.compose.ui.platform.PlatformTextInputMethodRequest

InterceptPlatformTextInput(
    interceptor = { request, nextHandler ->
        // Create a new request to wrap the incoming one with some custom logic.
        val modifiedRequest =
            object : PlatformTextInputMethodRequest {
                override fun createInputConnection(outAttributes: EditorInfo): InputConnection {
                    val inputConnection = request.createInputConnection(outAttributes)
                    // After the original request finishes initializing the EditorInfo we can
                    // customize it. If we needed to we could also wrap the InputConnection
                    // before
                    // returning it.
                    updateEditorInfo(outAttributes)
                    return inputConnection
                }

                fun updateEditorInfo(outAttributes: EditorInfo) {
                    // Your code here, e.g. set some custom properties.
                }
            }

        // Send our wrapping request to the next handler, which could be the system or another
        // interceptor up the tree.
        nextHandler.startInputMethod(modifiedRequest)
    }
) {
    BasicTextField(rememberTextFieldState())
}
```

```kotlin
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.InterceptPlatformTextInput

/**
 * A function that disables the soft keyboard for any text field within its content.
 *
 * The keyboard is re-enabled by removing this modifier or passing `disable = false`.
 */
@Composable
fun DisableSoftKeyboard(disable: Boolean = true, content: @Composable () -> Unit) {
    InterceptPlatformTextInput(
        interceptor = { request, nextHandler ->
            // If this flag is changed while an input session is active, a new lambda instance
            // that captures the new value will be passed to InterceptPlatformTextInput, which
            // will automatically cancel the session upstream and restart it with this new
            // interceptor.
            if (!disable) {
                // Forward the request to the system.
                nextHandler.startInputMethod(request)
            } else {
                // This function has to return Nothing, and since we don't have any work to do
                // in this case, we just suspend until cancelled.
                awaitCancellation()
            }
        },
        content = content,
    )
}
```