JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
14.85k stars 1.08k forks source link

Is there any easy way to help me intercept the return key? #4724

Closed Oyama-Oyama closed 2 weeks ago

Oyama-Oyama commented 2 weeks ago

Describe the problem Explain the performance issue you're experiencing, including the following details:

Affected platforms Select one of the platforms below:

If the problem is Android-only, report it in the Jetpack Compose tracker

Versions

Sample code If possible, provide a small piece of code that reproduces the problem. If the code snippet is too large to paste here, please link to a Gist, a GitHub repo, or any other public code repository.

Reproduction steps Please provide a detailed step-by-step guide on how to reproduce the issue you are experiencing.

Video If you're reporting slow app work or missing frames, please provide a video of the problem.

Profiling data Please provide any relevant profiling data that might be helpful. This could include information like FPS, memory usage, CPU time, or any other data that could provide insight into the performance issue.

Additional information Provide any other details that you think might be helpful for us to understand the problem. This could include things like the system configuration, external factors, etc.

igordmn commented 2 weeks ago

Use this:

        var text by remember { mutableStateOf("") }
        TextField(text, { text = it }, Modifier.onPreviewKeyEvent {
            if (it.key == Key.Enter) {
                println("Enter")
                true
            } else {
                false
            }
        })
Oyama-Oyama commented 1 week ago

This method has no effect when using NavController

igordmn commented 1 week ago

Could you provide a snippet with NavController where it doesn't work?

Oyama-Oyama commented 1 week ago
val navController = rememberNavController()
SudokuTheme(appTheme = appTheme) {
    NavHost(
        navController = navController,
        startDestination = "test",
        modifier = Modifier.fillMaxSize(1.0f).onPreviewKeyEvent {
            println("key::${it.key.keyCode}")
            true
        }
    ) {

    }
}
igordmn commented 1 week ago

onPreviewKeyEvent triggers only if some element inside is focused.

See the snippet in https://github.com/JetBrains/compose-multiplatform/issues/4764 how to request a focus.

This is also the task for simplifying this case.

Oyama-Oyama commented 1 week ago

Functioning normally, thanks