Hey! Does it possible to catch raw keyboard events...
# compose
u
Hey! Does it possible to catch raw keyboard events in textfield including ENTER / DELETE in Jetpack? I need to get the raw pressed key codes I tried
Copy code
TextField(
                value = "",
                onValueChange = {

                },
                label = { Text("Label") },
                modifier = Modifier.onKeyEvent { event ->
                    println(event)
                    true
                }
            )
But it doesn't print event when I type
s
Are you sure that the
TextField
is focused?
.onKeyEvent
should work. If you want to prevent the event from being handled elsewhere, use
.onPreviewKeyEvent
u
I'm pretty sure it's focused, I simply tap it in my phone and the keyboard opens, but then when I type
.onKeyEvent
doesn't being called at all
Copy code
@Composable
fun MainScreen() {
    TextField(
        value = "",
        onValueChange = {},
        modifier = Modifier
            .onKeyEvent {
                println("on key event")
                true
            }
    )
}
This is how it looks like, the event never printed in logcat
z
You’re using the software keyboard? onKeyEvent is for hardware key events
The only way to get soft keys is to see how the text changed in onValueChange. In the new text field apis you can use an InputTransformation to see exactly what changed
u
I use the software keyboard. my app is heavily based on this feature, I send the keycodes (as scanCodes) which I type to remote device. Is there a workaround for getting the event similar to onKeyEvent so I can access the scanCode / keyCode and also get event also for Backspace / Enter?
m
onKeyEvent is for hardware key events
I'm currently working on a Compose Multiplatform project and I noticed that
onKeyEvent
works fine for the software keys on Android. I'm trying to detect the backspace key event. On iOS however,
onKeyEvent
is only triggered via a physical keyboard
u
@Mofe Ejegi Did you found a workaround? I paused my project because of it, I need to get the event and know which keys pressed in the virtual keyboard in reliable way