foseja
foseja

Reputation: 351

Android Jetpack Compose: Listen to user's keyboard input not working with Enter key

I am trying to execute a function upon Enter Key pressed on soft keyboard and I found Modifier.onKeyEvent{} to listen to user input on soft keyboard in general.

However, this doesn't work with Enter key(especially 'Done' key on number pad).

My soft keyboard looks like this

enter image description here

with this option below in TextField

keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number)

when I print logs on any key pressed, all other keys press are recognized but those 'Done' and '.-' keys.

Upvotes: 5

Views: 3779

Answers (2)

Phil Dukhov
Phil Dukhov

Reputation: 87615

You can handle it with keyboardActions text field argument:

TextField(
    value = text, onValueChange = { text = it },
    keyboardOptions = KeyboardOptions.Default.copy(
        keyboardType = KeyboardType.Number,
        imeAction = ImeAction.Done
    ),
    keyboardActions = KeyboardActions(onDone = {
        println("done")
    })
)

Upvotes: 6

Gabe Sechan
Gabe Sechan

Reputation: 93542

The enter key is special. Especially when it's not an enter key, but a Done button or similar. Instead of sending a commitText, it send it via the OnEditorActionListener of the view. You need to set an OnEditorActionListener and handle the case in that.

Upvotes: 1

Related Questions