Nucleus
Input

Keyboard

Read key events with the Compose key modifiers, and tell a held key's auto-repeats from a new press.

View as Markdown

Key events reach Compose through the standard Modifier.onKeyEvent and Modifier.onPreviewKeyEvent, and you add nothing beyond nucleus-application. Nucleus adds one property to Compose's KeyEvent: whether a key-down is an auto-repeat.

Ignore auto-repeatNew

While the user holds a key, the OS sends key-down after key-down. KeyEvent.isRepeat is true for those repeats, so a held shortcut acts once:

import dev.nucleusframework.window.ExperimentalNucleusApi
import dev.nucleusframework.window.tao.isRepeat

@OptIn(ExperimentalNucleusApi::class)
fun Modifier.closeTabShortcut(onClose: () -> Unit): Modifier =
    onPreviewKeyEvent { event ->
        if (event.type == KeyEventType.KeyDown && event.isCtrlPressed && event.key == Key.W) {
            if (!event.isRepeat) onClose()
            true
        } else {
            false
        }
    }

Without the check, holding Ctrl+W closes one tab per repeat.

  • Only a KeyDown is ever a repeat. Key-ups and the event that carries the typed character report false, and text fields repeat characters as usual.
  • isRepeat is false for an event Nucleus did not produce, such as one built in a test.
  • It works in windows and in native popups, on macOS, Windows and Linux (X11 and Wayland).
  • isRepeat is @ExperimentalNucleusApi.

Notes

  • On Linux, a window enables its input method only while a text field has focus. A Wayland compositor with an on-screen keyboard, such as Plasma Mobile, shows the keyboard for text entry only, not each time a window becomes active.

What's next

Was this page helpful?
Edit on GitHub

Last updated on