Input
Keyboard
Read key events with the Compose key modifiers, and tell a held key's auto-repeats from a new press.
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
KeyDownis ever a repeat. Key-ups and the event that carries the typed character reportfalse, and text fields repeat characters as usual. isRepeatisfalsefor 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).
isRepeatis@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
- Global hotkeys: shortcuts that work while the app is in the background.
- Mouse buttons: the back and forward side buttons.
- Accessibility: expose your UI to screen readers.
Was this page helpful?
Edit on GitHub
Last updated on