TextField does not stop key event propagation to parent/window event handlers
Typing a key into a TextField will continue to propagate the key event to any parent composables using onKeyEvent or the window onKeyEvent, as far as I can tell. An example can be found here. The documentation for Window()'s onKeyEvent parameter seems to suggest that individual UI elements can consume key events before it reaches the window-level handler and it is explicitly stated in Modifier.onKeyEvent(), but this doesn't work for TextField input (and others as well, although TextField is the only one I've seen it with).
For my specific use case, I'd like to capture space key events anywhere in the application to play/pause audio, but only if the user doesn't have a TextField focused. I haven't been able to find a way to do this with either of these methods - composable or window-based - since it's a terrible experience to play/pause audio if the user adds a space to a text field.
Not very friendly indeed
individual UI elements can consume key events before it reaches the window-level handler and it is explicitly stated in Modifier.onKeyEvent()
In your example, there is no Modifier.onKeyEvent { true } declared for the TextFields.
TextField(
modifier = Modifier.onKeyEvent { true },
....
If it's declared, the input events do not propagate up.
Does it help when you try it?
Sorry for the very late response - yes, adding onKeyEvent { true } does seem to fix the problem. I'm not sure why I was returning false in the example I linked. So this is a workaround, but it seems like it should be the default behavior for TextFields to avoid requiring this in every text field.
The logic appears to be coming from TextFieldKeyInput which for example seems to only consume KeyDown events (although many KeyDown events from typing still make it to my global key event handler).
The .onKeyEvent { true } works but prevents the Tab key to switch to the next input.
.onKeyEvent { it.key != Key.Tab } may do the trick.
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.