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

KeyEvent

value public final class KeyEvent


When a user presses a key on a hardware keyboard, a KeyEvent is sent to the item that is currently focused. Any parent composable can intercept this key event on its way to the focused item by using Modifier.onPreviewKeyEvent()]onPreviewKeyEvent. If the item is not consumed, it returns back to each parent and can be intercepted by using Modifier.onKeyEvent()]onKeyEvent.

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()
    )
}

Summary

Public constructors

KeyEvent(@NonNull NativeKeyEvent nativeKeyEvent)

Public methods

final @NonNull NativeKeyEvent

Extension functions

final @NonNull Key

The key that was pressed.

final @NonNull KeyEventType

The type of key event.

final int

The UTF16 value corresponding to the key event that was pressed.

final boolean

Indicates whether the Alt key is pressed.

final boolean

Indicates whether the Ctrl key is pressed.

final boolean

Indicates whether the Meta key is pressed.

final boolean

Indicates whether the Shift key is pressed.

Public constructors

KeyEvent

public KeyEvent(@NonNull NativeKeyEvent nativeKeyEvent)

Public methods

getNativeKeyEvent

public final @NonNull NativeKeyEvent getNativeKeyEvent()

Extension functions

KeyEventKt.getKey

public final @NonNull Key KeyEventKt.getKey(@NonNull KeyEvent receiver)

The key that was pressed.

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.isAltPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent

Box(
    Modifier
        .onKeyEvent {
            if (it.isAltPressed && it.key == Key.A) {
                println("Alt + A is pressed")
                true
            } else {
                false
            }
        }
        .focusable()
)

KeyEventKt.getType

public final @NonNull KeyEventType KeyEventKt.getType(@NonNull KeyEvent receiver)

The type of key event.

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.KeyEventType.Companion.KeyDown
import androidx.compose.ui.input.key.KeyEventType.Companion.KeyUp
import androidx.compose.ui.input.key.KeyEventType.Companion.Unknown
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type

Box(
    Modifier
        .onKeyEvent {
            when (it.type) {
                KeyUp -> println(" KeyUp Pressed")
                KeyDown -> println(" KeyUp Pressed")
                Unknown -> println("Unknown key type")
                else -> println("New KeyTpe (For Future Use)")
            }
            false
        }
        .focusable()
)

KeyEventKt.getUtf16CodePoint

public final int KeyEventKt.getUtf16CodePoint(@NonNull KeyEvent receiver)

The UTF16 value corresponding to the key event that was pressed. The unicode character takes into account any meta keys that are pressed (eg. Pressing shift results in capital alphabets). The UTF16 value uses the U+n notation[http://www.unicode.org/reports/tr27/#notation] of the Unicode Standard.

An Int is used instead of a Char so that we can support supplementary characters. The Unicode Standard allows for characters whose representation requires more than 16 bits. The range of legal code points is U+0000 to U+10FFFF, known as Unicode scalar value.

The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).

KeyEventKt.isAltPressed

public final boolean KeyEventKt.isAltPressed(@NonNull KeyEvent receiver)

Indicates whether the Alt key is pressed.

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.isAltPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent

Box(
    Modifier
        .onKeyEvent {
            if (it.isAltPressed && it.key == Key.A) {
                println("Alt + A is pressed")
                true
            } else {
                false
            }
        }
        .focusable()
)

KeyEventKt.isCtrlPressed

public final boolean KeyEventKt.isCtrlPressed(@NonNull KeyEvent receiver)

Indicates whether the Ctrl key is pressed.

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.isCtrlPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent

Box(
    Modifier
        .onKeyEvent {
            if (it.isCtrlPressed && it.key == Key.A) {
                println("Ctrl + A is pressed")
                true
            } else {
                false
            }
        }
        .focusable()
)

KeyEventKt.isMetaPressed

public final boolean KeyEventKt.isMetaPressed(@NonNull KeyEvent receiver)

Indicates whether the Meta key is pressed.

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.isMetaPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent

Box(
    Modifier
        .onKeyEvent {
            if (it.isMetaPressed && it.key == Key.A) {
                println("Meta + A is pressed")
                true
            } else {
                false
            }
        }
        .focusable()
)

KeyEventKt.isShiftPressed

public final boolean KeyEventKt.isShiftPressed(@NonNull KeyEvent receiver)

Indicates whether the Shift key is pressed.

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.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent

Box(
    Modifier
        .onKeyEvent {
            if (it.isShiftPressed && it.key == Key.A) {
                println("Shift + A is pressed")
                true
            } else {
                false
            }
        }
        .focusable()
)