Enahor Tapmas
05/19/2024, 9:39 PMBox(modifier = Modifier.fillMaxSize().paint(
painter = BitmapPainter(loadImageBitmap(File(IMG_NAME_ARR[min(anim , 120)]).inputStream())),
).focusRequester(requester)
.focusable()
.onKeyEvent {
if (it.key == Key.A) {
anim = if (anim <=0) 120 else anim -1
} else if (it.key == Key.D) {
anim = if (anim >= 120) 0 else anim + 1
}
true
}
)
Stylianos Gakis
05/19/2024, 9:52 PMAdding this modifier to the modifier parameter of a component will allow it to intercept hardware key events when it (or one of its children) is focused.
And no children are focused, and this item itself becomes focusable before the onKeyEvent is set.
The docs have it the other way around https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).onKeyEvent(kotlin.Function1) the onKeyEvent
is set, and then the item becomes focusable.
Can you flip them and see if anything changes?Enahor Tapmas
05/19/2024, 10:55 PMvar requester = remember { FocusRequester() }
Box(modifier = Modifier.fillMaxWidth().fillMaxHeight().paint(
painter = BitmapPainter(loadImageBitmap(File(IMG_NAME_ARR[min(anim , 120)]).inputStream())),
)
.onKeyEvent {
if (it.key == Key.A) {
anim = if (anim <=0) 120 else anim -1
} else if (it.key == Key.D) {
anim = if (anim >= 120) 0 else anim + 1
}
false
}
.focusRequester(requester)
.focusable()
)
Text("$anim", modifier = Modifier )
}
Enahor Tapmas
05/19/2024, 10:55 PMStylianos Gakis
05/19/2024, 11:27 PMEnahor Tapmas
05/19/2024, 11:33 PMStylianos Gakis
05/20/2024, 7:44 AM