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
15.24k stars 1.11k forks source link

Be able to define define keyboard event handlers that are always active in the current window, globally. #4851

Closed Zenturas closed 1 month ago

Zenturas commented 1 month ago

``e.g. using the window object after calling singleWindowApplication.

Currently, you can define keyboard event handlers during the call to singleWindowApplication, but afterward, it's impossible. I'd like the ability to hook up and add/remove keyboard handlers even after calling singleWindowApplication for this window.

Like for mouse event handling you can

@Composable
fun WindowsMouseEventBackHandler(
    composeWindow: ComposeWindow,
    navController: NavController
) {
    val mouseListener = object : MouseListener {
        override fun mouseClicked(event: MouseEvent?) {}
        override fun mousePressed(event: MouseEvent?) {}
        override fun mouseEntered(event: MouseEvent?) {}
        override fun mouseExited(event: MouseEvent?) {}
        override fun mouseReleased(event: MouseEvent?) {
            event?.let {
                if (it.button == 4) {
                    // todo navigateUp
                }
            }
        }
    }

    LaunchedEffect(Unit) {
        composeWindow.addMouseListener(mouseListener)
    }

    DisposableEffect(Unit) {
        onDispose {
            composeWindow.removeMouseListener(mouseListener)
        }
    }
}
igordmn commented 1 month ago

There is a cumbersome way mentioned in https://github.com/JetBrains/compose-multiplatform/issues/4764. Please vote for it, if it is useful for you to have a simple API.