{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

SoftwareKeyboardInterceptionModifierKt

public final class SoftwareKeyboardInterceptionModifierKt


Summary

Public methods

static final @NonNull Modifier
@ExperimentalComposeUiApi
onInterceptKeyBeforeSoftKeyboard(
    @NonNull Modifier receiver,
    @NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onInterceptKeyBeforeSoftKeyboard
)

Adding this modifier to the modifier parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard.

static final @NonNull Modifier
@ExperimentalComposeUiApi
onPreInterceptKeyBeforeSoftKeyboard(
    @NonNull Modifier receiver,
    @NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onPreInterceptKeyBeforeSoftKeyboard
)

Adding this modifier to the modifier parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard.

Public methods

onInterceptKeyBeforeSoftKeyboard

@ExperimentalComposeUiApi
public static final @NonNull Modifier onInterceptKeyBeforeSoftKeyboard(
    @NonNull Modifier receiver,
    @NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onInterceptKeyBeforeSoftKeyboard
)

Adding this modifier to the modifier parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard.

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
@NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onInterceptKeyBeforeSoftKeyboard

This callback is invoked when the user interacts with the hardware keyboard. While implementing this callback, return true to stop propagation of this event. If you return false, the key event will be sent to this SoftKeyboardInterceptionModifierNode's parent, and ultimately to the software keyboard.

onPreInterceptKeyBeforeSoftKeyboard

@ExperimentalComposeUiApi
public static final @NonNull Modifier onPreInterceptKeyBeforeSoftKeyboard(
    @NonNull Modifier receiver,
    @NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onPreInterceptKeyBeforeSoftKeyboard
)

Adding this modifier to the modifier parameter of a component will allow it to intercept hardware key events before they are sent to the software keyboard. This modifier is similar to onInterceptKeyBeforeSoftKeyboard, but allows a parent composable to intercept the hardware key event before any child.

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
@NonNull Function1<@NonNull KeyEvent, @NonNull Boolean> onPreInterceptKeyBeforeSoftKeyboard

This callback is invoked when the user interacts with the hardware keyboard. It gives ancestors of a focused component the chance to intercept a KeyEvent. Return true to stop propagation of this event. If you return false, the key event will be sent to this SoftKeyboardInterceptionModifierNode's child. If none of the children consume the event, it will be sent back up to the root KeyInputModifierNode using the onKeyEvent callback, and ultimately to the software keyboard.