I have a single Scaffold for the entire UI of my p...
# compose-desktop
z
I have a single Scaffold for the entire UI of my program. The issue is the onKeyEvent doesnt run until i click around on the scaffold (I'm assuming to focus it). But I want it to always pick up the key events no matter what, shouldn't the Scaffold, being the root component get the events no matter whats focused? This is what I have, minus the actual key - function logic
Copy code
Scaffold(
    modifier = Modifier.onKeyEvent {
        if (it.type != KeyEventType.KeyUp) return@onKeyEvent false

        // ...

        true
    }
}
e
events stop propagating once they are consumed (e.g. by a handler returning
true
)
j
I just a
FocusRequestor
for this, requesting the focus on my main element in a
LaunchedEffect(Unit)
.
a
Or put use onKeyEvent on the window.
z
@ephemient I see, but that's the only handler in my entire program, so nothing else should be stopping it propagating
Window level onKeyEvent won't work for my use-case as I use the viewModel for several of the key bindings. And the viewModel isn't available at that early:
java.lang.IllegalStateException: No ViewModelStoreOwner was provided via LocalViewModelStoreOwner
e
other focusable elements also consume key events
you can use
Modifier.onPreviewKeyEvent
to observe it on the downward pass instead of the upward pass
c
interesting. i recently had a similar issue in my android app. similarly to Nicholas " I see, but that's the only handler in my entire program, so nothing else should be stopping it propagating" im still not sure what was possibly consuming my events. i ended up having to put the key listeners on my activity. But I'll try the onPreviewKeyEvent to see if that works there. 🤞