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

Key

value public final class Key


Represents keys on a 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.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()
)

Summary

Nested types

public static class Key.Companion

Public constructors

Key(long keyCode)
Key(long keyCode)

Public methods

final long

a Long value representing the key pressed.

final long

an integer code representing the key pressed.

@NonNull String

Extension functions

final int

The native keycode corresponding to this Key.

Public constructors

Key

public Key(long keyCode)
Parameters
long keyCode

a Long value representing the key pressed. Note: This keycode can be used to uniquely identify a hardware key. It is different from the native keycode.

Key

public Key(long keyCode)
Parameters
long keyCode

an integer code representing the key pressed.

Public methods

getKeyCode

public final long getKeyCode()

a Long value representing the key pressed. Note: This keycode can be used to uniquely identify a hardware key. It is different from the native keycode.

getKeyCode

public final long getKeyCode()

an integer code representing the key pressed.

toString

public @NonNull String toString()

Extension functions

KeyKt.getNativeKeyCode

public final int KeyKt.getNativeKeyCode(@NonNull Key receiver)

The native keycode corresponding to this Key.