24

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 = {}
        )
)
1
  • 3
    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. Commented May 3, 2022 at 21:04

3 Answers 3

34

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),
)
Sign up to request clarification or add additional context in comments.

7 Comments

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?
@ArcRuler Use val focusManager = LocalFocusManager.current and in the Modifier.onKeyEvent of the 2nd field: focusManager.clearFocus()
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 I guess this is Done button on on-screen keyboard. GabrieleMariotti I guess, did you miss "else" before "false"?
Looks like singleLine = true and true in onKeyEvent don't help: newline is added to output variable.
|
1

For Windows / Desktop platform with KMP

You need

                singleLine = true,
                modifier = Modifier.onKeyEvent { event ->
                    return@onKeyEvent if (event.key.keyCode == Key.Enter.keyCode){
                        viewModel.login(userName, userName)
                        true
                    } else {
                        false
                    }
                }

example

           TextField(
                value = userName,
                onValueChange = { userName = it },
                label = { Text("Username") },
                keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                keyboardActions = KeyboardActions(onDone = {
                    viewModel.login(userName, userName)
                }),
                singleLine = true,
                modifier = Modifier.onKeyEvent { event ->
                    return@onKeyEvent if (event.key.keyCode == Key.Enter.keyCode){
                        viewModel.login(userName, userName)
                        true
                    } else {
                        false
                    }
                }
            )

Comments

0

If you don't want the key pressed to be included in onValueChange you can use the onPreviewKeyEvent modifier to intercept the key press and do an action

example:

    modifier = Modifier
        .onPreviewKeyEvent {
            if (it.key == Key.Enter && it.nativeKeyEvent.action == ACTION_DOWN) {
                //do action
                true
            } else {
                false
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.