https://kotlinlang.org logo
#compose
Title
# compose
a

Alexa_Gal

11/24/2021, 4:20 PM
hi guys, do you know how can i detect when the
back
button has been pressed? i tried with all the
KeyboardActions
but none of them are triggered when i press that button 😕 (using BasicTextField)
r

rook

11/24/2021, 4:24 PM
a

Alexa_Gal

11/24/2021, 4:43 PM
i tried with this and didnt work
Copy code
.onKeyEvent {
                    if (it.nativeKeyEvent.keyCode == KEYCODE_DEL) {
                        onBack()
                    }
                    true
                }
r

rook

11/24/2021, 5:06 PM
Add a log to the top of that block and record the key code in the log. When you press the key you want, you’ll get the code back and you can register for that key press.
I recommend against setting that delete key as the back button, for whatever it’s worth
a

Alexa_Gal

11/24/2021, 5:08 PM
it doesn’t trigger the block, it does to alll other keys but no to
KEYCODE_DEL
😞
Copy code
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
            modifier = Modifier
                .align(Alignment.Center)
                .onKeyEvent {
                    Log.e("key", "${it.nativeKeyEvent.keyCode}")
                    if (it.nativeKeyEvent.keyCode == KEYCODE_DEL) {
                        onBack()
                    }
                    true
                }
                .focusRequester(focusRequester)
r

rook

11/24/2021, 5:10 PM
Well, the other route is to use imeOptions to make the actual back button available on the keyboard: https://developer.android.com/reference/android/widget/TextView#attr_android:imeOptions
actionPrevious
might be the one you’re looking for
7 Views