最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Key event not received outside popup component in Jetpack Compose - Stack Overflow

programmeradmin2浏览0评论
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)
                    }
            )
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论