admin 管理员组

文章数量: 1086019

Box(
    Modifier
        .onKeyEvent({
            Log.d("my_log", "Box key event ${it.nativeKeyEvent.keyCode}")
            return@onKeyEvent false
        })
        .focusable(true)
) {
    Popup(
        properties = PopupProperties(focusable = true)
    ) {
        Box(
            modifier = Modifier
                .size(50.dp)
                .background(MaterialTheme.colorScheme.background)
                .border(BorderStroke(2.dp, Color.Black))
                .onFocusChanged { state -> Log.d("my_log", "Popup Box 1 $state") }
                .clickable(onClick = {
                    Log.d("my_log", "Popup Box 1 click")
                })
                .onKeyEvent { event ->
                    Log.d("my_log", "Popup Box 1 key event ${event.nativeKeyEvent.keyCode}")
                    return@onKeyEvent false
                })
    }
}

The parent does not receive the key event not handled inside the Popup because the popup creates a new window. I need a way to pass the unhandled key event to the parent element outside the popup.

From the above sample, the log inside the popup will be printed during key event and the log in the box is not printed. It is because the key events is blocked and not reaching outside the popup.

Box(
    Modifier
        .onKeyEvent({
            Log.d("my_log", "Box key event ${it.nativeKeyEvent.keyCode}")
            return@onKeyEvent false
        })
        .focusable(true)
) {
    Popup(
        properties = PopupProperties(focusable = true)
    ) {
        Box(
            modifier = Modifier
                .size(50.dp)
                .background(MaterialTheme.colorScheme.background)
                .border(BorderStroke(2.dp, Color.Black))
                .onFocusChanged { state -> Log.d("my_log", "Popup Box 1 $state") }
                .clickable(onClick = {
                    Log.d("my_log", "Popup Box 1 click")
                })
                .onKeyEvent { event ->
                    Log.d("my_log", "Popup Box 1 key event ${event.nativeKeyEvent.keyCode}")
                    return@onKeyEvent false
                })
    }
}

The parent does not receive the key event not handled inside the Popup because the popup creates a new window. I need a way to pass the unhandled key event to the parent element outside the popup.

From the above sample, the log inside the popup will be printed during key event and the log in the box is not printed. It is because the key events is blocked and not reaching outside the popup.

Share Improve this question asked Mar 28 at 19:09 Sivabharath KSivabharath K 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can use this.

@Composable
fun MyComposable() {
    val parentKeyEventHandler: (KeyEvent) -> Boolean = remember {
        { event ->
            Log.d("my_log", "Box key event ${event.nativeKeyEvent.keyCode}")
            false // Allow further propagation if needed
        }
    }

    Box(
        Modifier
            .onKeyEvent(parentKeyEventHandler) // Parent Key Event Handler
            .focusable(true)
    ) {
        Popup(
            properties = PopupProperties(focusable = true)
        ) {
            Box(
                modifier = Modifier
                    .size(50.dp)
                    .background(MaterialTheme.colorScheme.background)
                    .border(BorderStroke(2.dp, Color.Black))
                    .onFocusChanged { state -> Log.d("my_log", "Popup Box 1 $state") }
                    .clickable(onClick = {
                        Log.d("my_log", "Popup Box 1 click")
                    })
                    .onKeyEvent { event ->
                        Log.d("my_log", "Popup Box 1 key event ${event.nativeKeyEvent.keyCode}")
                        // If the event is not handled, forward it to the parent
                        parentKeyEventHandler(event)
                    }
            )
        }
    }
}

本文标签: androidKey event not received outside popup component in Jetpack ComposeStack Overflow