# KeyboardActions

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/KeyboardActions.kt+class:androidx.compose.foundation.text.KeyboardActions) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0) Common/AllAndroid/JVM
- Cmn

  ```
  class KeyboardActions
  ```

*** ** * ** ***

Configures custom actions to run when the action button on the software keyboard (such as the "Done", "Search", or "Next" button in the bottom corner of the keyboard) is clicked.

Setting these actions overrides default behavior, allowing you to implement custom focus routing (e.g., moving focus to a specific field on Next) or trigger custom logic (e.g., hiding the keyboard or submitting data on Done).

Use `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope#defaultKeyboardAction(androidx.compose.ui.text.input.ImeAction)` to run the default action for the triggered `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction`.

```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextObfuscationMode
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

val nameState = rememberTextFieldState()
val emailState = rememberTextFieldState()
val passwordState = rememberTextFieldState()
var isPasswordVisible by remember { mutableStateOf(false) }
val urlState = rememberTextFieldState()
val addressState = rememberTextFieldState()
val biographyState = rememberTextFieldState()

Column(
    verticalArrangement = Arrangement.spacedBy(12.dp),
    modifier = Modifier.padding(16.dp).fillMaxWidth(),
) {
    // 1. PersonName Input (Auto-Capitalizes Words)
    OutlinedTextField(
        state = nameState,
        label = { Text("Full Name") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.PersonName,
                capitalization = KeyboardCapitalization.Words,
                imeAction = ImeAction.Next, // Soft keyboard automatically moves focus on Next
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 2. Email Input (Restricted layout showing '@' and '.', auto-capitalization disabled)
    OutlinedTextField(
        state = emailState,
        label = { Text("Email") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Email,
                capitalization = KeyboardCapitalization.None,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 3. Password Input with Eye-Icon Toggle (masked vs visible password layouts,
    // capitalizations disabled)
    Row(verticalAlignment = Alignment.CenterVertically) {
        OutlinedSecureTextField(
            state = passwordState,
            label = { Text("Password") },
            textObfuscationMode =
                if (isPasswordVisible) TextObfuscationMode.Visible
                else TextObfuscationMode.System,
            keyboardOptions =
                KeyboardOptions(
                    keyboardType =
                        if (isPasswordVisible) KeyboardType.PasswordVisible
                        else KeyboardType.Password,
                    capitalization = KeyboardCapitalization.None,
                    imeAction = ImeAction.Next,
                ),
            modifier = Modifier.weight(0.7f),
        )
        Spacer(Modifier.width(8.dp))
        TextButton(
            onClick = { isPasswordVisible = !isPasswordVisible },
            modifier = Modifier.weight(0.3f),
        ) {
            Text(if (isPasswordVisible) "Hide" else "Show")
        }
    }

    // 4. Uri Input (Prominent '/' and '.com' keys, auto-capitalization disabled)
    OutlinedTextField(
        state = urlState,
        label = { Text("Homepage URL") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Uri,
                capitalization = KeyboardCapitalization.None,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 5. PostalAddress Input (Auto-Capitalizes Words, prompts standard shipping keys)
    OutlinedTextField(
        state = addressState,
        label = { Text("Shipping Address") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.PostalAddress,
                capitalization = KeyboardCapitalization.Words,
                imeAction = ImeAction.Next,
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    // 6. Free-Text Biography Input (Auto-Capitalizes Sentences, multi-line active)
    OutlinedTextField(
        state = biographyState,
        label = { Text("Biography") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Text,
                capitalization = KeyboardCapitalization.Sentences,
                imeAction =
                    ImeAction.Done, // Soft keyboard automatically dismisses keyboard on Done
            ),
        modifier = Modifier.fillMaxWidth(),
    )
}
```

```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.OutlinedSecureTextField
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

val emailState = rememberTextFieldState()
val passwordState = rememberTextFieldState()

Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
    OutlinedTextField(
        state = emailState,
        label = { Text("Email Address") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Email,
                capitalization = KeyboardCapitalization.None,
                imeAction =
                    ImeAction
                        .Next, // Soft keyboard automatically shifts focus to password field on
                // Next
            ),
        lineLimits = TextFieldLineLimits.SingleLine,
        modifier = Modifier.fillMaxWidth(),
    )

    OutlinedSecureTextField(
        state = passwordState,
        label = { Text("Password") },
        keyboardOptions =
            KeyboardOptions(
                keyboardType = KeyboardType.Password,
                capitalization = KeyboardCapitalization.None,
                imeAction =
                    ImeAction.Done, // Soft keyboard automatically dismisses the keyboard on Done
            ),
        modifier = Modifier.fillMaxWidth(),
    )
}
```

## Summary

| ### Public companion properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#Default()` Use this default value if you don't want to specify any action but want to use the default action implementations. | Cmn |

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#KeyboardActions(kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1,kotlin.Function1)( onDone: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, onGo: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, onNext: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, onPrevious: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, onSearch: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, onSend: (https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? )` | Cmn |

| ### Public functions |
|---|---|---|
| `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#equals(kotlin.Any)(other: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?)` | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#hashCode()()` | Cmn |

| ### Public properties |
|---|---|---|
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onDone()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Done()` action. | Cmn |
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onGo()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Go()` action. | Cmn |
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onNext()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Next()` action. | Cmn |
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onPrevious()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Previous()` action. | Cmn |
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onSearch()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Search()` action. | Cmn |
| `(https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActionScope.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/KeyboardActions#onSend()` This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Send()` action. | Cmn |

## Public companion properties

### Default

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val Default: KeyboardActions
```

Use this default value if you don't want to specify any action but want to use the default action implementations.

## Public constructors

### KeyboardActions

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
KeyboardActions(
    onDone: (KeyboardActionScope.() -> Unit)? = null,
    onGo: (KeyboardActionScope.() -> Unit)? = null,
    onNext: (KeyboardActionScope.() -> Unit)? = null,
    onPrevious: (KeyboardActionScope.() -> Unit)? = null,
    onSearch: (KeyboardActionScope.() -> Unit)? = null,
    onSend: (KeyboardActionScope.() -> Unit)? = null
)
```

## Public functions

### equals

Cmn

```
open operator fun equals(other: Any?): Boolean
```

### hashCode

Cmn

```
open fun hashCode(): Int
```

## Public properties

### onDone

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onDone: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Done()` action. A null value indicates that the default implementation if any, should be executed.

### onGo

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onGo: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Go()` action. A null value indicates that the default implementation if any, should be executed.

### onNext

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onNext: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Next()` action. A null value indicates that the default implementation should be executed. The default implementation moves focus to the next item in the focus traversal order.

See `https://developer.android.com/reference/kotlin/androidx/compose/ui/focus/focusProperties.modifier#(androidx.compose.ui.Modifier).focusProperties(kotlin.Function1)` for more details on how to specify a custom focus order if needed.

### onPrevious

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onPrevious: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Previous()` action. A null value indicates that the default implementation should be executed. The default implementation moves focus to the previous item in the focus traversal order.

See `https://developer.android.com/reference/kotlin/androidx/compose/ui/focus/focusProperties.modifier#(androidx.compose.ui.Modifier).focusProperties(kotlin.Function1)` for more details on how to specify a custom focus order if needed.

### onSearch

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onSearch: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Search()` action. A null value indicates that the default implementation if any, should be executed.

### onSend

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.0.0)

```
val onSend: (KeyboardActionScope.() -> Unit)?
```

This is run when the user triggers the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/ImeAction#Send()` action. A null value indicates that the default implementation if any, should be executed.