question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why keyboard action will invoke twice but i only trigger once?

See original GitHub issue

When i test keyboard simple example, i find a problem that the action will invoke twice but i only trigger once.

My code is simple and from: https://github.com/JetBrains/compose-jb/blob/master/tutorials/Keyboard/README.md

fun main() = Window {
    MaterialTheme {
        var text by remember { mutableStateOf("") }
        Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
            TextField(
                value = text,
                onValueChange = { text = it },
                modifier = Modifier.onPreviewKeyEvent {
                    when {
                        (it.isCtrlPressed && it.key == Key.Minus) -> {
                            println("on enter")
                            true
                        }
                        else -> false
                    }
                }
            )
        }
    }
}

here is the recording, you can see the “on enter” print twice when i trigger once Screen Recording 2022-02-19 at 3 13 01 AM

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ryanmitchenercommented, Feb 21, 2022

@akurasov I have added a PR for updating the keyboard tutorial here: https://github.com/JetBrains/compose-jb/pull/1877

1reaction
RubiTreecommented, Feb 19, 2022

@RubiTree I believe you are getting the KeyDown and KeyUp events. Add a check for keydown or keyup depending on when you want the event to trigger.

when {
    (it.type == KeyEventType.KeyDown && it.isCtrlPressed && it.key == Key.Minus) -> {
        println("on enter")
        true
    }
    else -> false
}

thank your for giving solution, really works ! But i wonder why not add the condition “it.type == KeyEventType.KeyDown” to sample?

Read more comments on GitHub >

github_iconTop Results From Across the Web

button trigger behaviour: "release only" triggers twice!?
The event will be triggered 2 times. First after the button crosses the presspoint with context.started = true and context.performed = false.
Read more >
Hotkey trigger tapped twice issue - Questions & Suggestions
I set Keyboard maestro to launch an app by tapping twice a letter. It's work but I can't use this letter to type...
Read more >
Why is onKey() called twice? - android - Stack Overflow
OnKey is fired twice: the first time for key down, and the second time for key up, so you have to filter: YOUR_VIEW.setOnKeyListener(new...
Read more >
Oops! 10 Keyboard Shortcuts Users Keep Hitting by Mistake
Accidentally pressed something on your keyboard and now you can't type properly? Here are fixes for common keyboard shortcut issues.
Read more >
How to correctly use preventDefault(), stopPropagation(), or ...
Here we have taken the click event and prevented its default behaviour using event.preventDefault() , then invoked the fileUpload() function. We ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found