Reputation: 351
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
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
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
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