How to handle a keyboard escape button correctly inside a regular app? It should trigger a back navigation if you follow the accessibility guidelines.
In the old Android way that would be handled like this:
class MainActivity: Activity
...
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
Log.d("T", "onKeyUp: KEYCODE_BACK")
onBackPressed()
}
return super.onKeyUp(keyCode, event)
}
How do you handle this in correctly in Jetpack Compose?
You can use keyboards modifiers like Modifier.onKeyEvent
For example, at the root of your application:
Box(
modifier = Modifier
.onKeyEvent {
if(it.key == Key.Escape) {
// Assuming you are using jetpack compose navigation
navController.popBackStack()
}
true
}
) {
// Your content
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.