How to cancel a dpad key event programmatically in jetpack compose?

342 Views Asked by At

I want to ignore a dpad key event in a specific location to keep the focus on the screen. I tried to simulate another key event to cancel the first key event i.e. to cancel dpad_up I called dispatchKeyEvent for dpad_down but it doesn't seem to work since the focus disappears after the dpad_up pressed. Can anyone help me with that?

1

There are 1 best solutions below

0
st920228 On

try this

Modifier.onKeyEvent {
    when (it.key.nativeKeyCode) {
        KeyEvent.KEYCODE_DPAD_UP -> {
            return@onKeyEvent true
        }
        else -> {}
    }
return@onKeyEvent false
}