12

From what I understand, there is an API for the Jetpack Compose Textfield for capturing Keyboard actions but I don't know what of this APIs that can capture the Enter-Input

The use-case of this capturing enter input is to enable to click Enter and try to go to the next TextField and keeping while keeping the keyboard open

OutlinedTextField(
    value = username.value,
    onValueChange = {
        username.value = it
        },
    keyboardActions = KeyboardActions(
        onDone = {},
        onGo = {},
        onNext = {},
        onPrevious ={},
        onSearch ={},
        onSend = {}
        )
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Arthur
  • 318
  • 1
  • 4
  • 11
  • 1
    Additional question: I can't seem to have a physical keyboard do an IME action. I need it to run my onDone / onSubmit after a physical keyboard presses enter, but instead it just creates a new line. – dessalines May 03 '22 at 21:04

1 Answers1

19

You can use something like:

val (focusRequester) = FocusRequester.createRefs()

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    singleLine = true,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { focusRequester.requestFocus() }
    ),
    modifier = Modifier.onKeyEvent {
        if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER){
            focusRequester.requestFocus()
            true
        }
        false
    }
)

TextField(
    value = text2,
    onValueChange = {
        text2 = it
    },
    modifier = Modifier.focusRequester(focusRequester),
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • thanks for this, I've tried adding this to the 2nd textfield `singleLine = true, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions( onDone = { focusRequester.requestFocus() } ), modifier = Modifier.onKeyEvent { if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER){ focusRequester.requestFocus() true } false }` to the second textfield and use `focusRequester.freeFocus()` instead but somehow it didn't remove the focus on the 2nd textfield any idea on this? – Arthur Jun 09 '21 at 08:09
  • 1
    @ArcRuler Use `val focusManager = LocalFocusManager.current` and in the `Modifier.onKeyEvent` of the 2nd field: `focusManager.clearFocus()` – Gabriele Mariotti Jun 09 '21 at 13:26
  • How would I run an IME action there? A physical keyboard enter does not trigger any IME action, and I need it to run onDone. – dessalines May 03 '22 at 21:01
  • @dessalines I guess this is Done button on on-screen keyboard. GabrieleMariotti I guess, did you miss "else" before "false"? – midenok Jun 29 '23 at 21:30
  • What's the point for onValueChange? – midenok Jun 29 '23 at 21:33
  • How to make TextField don't lose focus after Enter? – midenok Jun 29 '23 at 21:33
  • Looks like `singleLine = true` and `true` in `onKeyEvent` don't help: newline is added to output variable. – midenok Jun 29 '23 at 21:38