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 11 Answer
Reset to default 0You 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
版权声明:本文标题:android - Key event not received outside popup component in Jetpack Compose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744017908a2519262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论