I click on the box but this will not give me ANY k...
# compose
e
I click on the box but this will not give me ANY keyboard events. Would be nice if someone could tell me why this would be wrong
Copy code
Box(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

    }
)
s
Idk if it is what you are experiencing but the docs say:
Copy code
Adding 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?
e
Copy code
var 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 )

}
this does nothing and I even tried it with the onprivew thing 😞
s
Even if it's a completely separate composable? Can you copy paste the sample from the docs directly and try running that as well? Also try to make sure you're on latest stable dependencies etc
e
oh whoops i should mention ive given up on trying to do kb/mouse with this framework. Honestly I just needed this for a quick testing thing but this framework makes it too difficult. Thanks for the help you have given though, appreciate it
s
No problem