I am trying to select the first clickable element ...
# compose
a
I am trying to select the first clickable element in a layout (Column) when 'Down' on the keyboard is pressed, when no elements are selected. However,
Modifier.onKeyEvent {}
requires the component or its children to have focus. I tried requesting focus on the Layout itself, but it automatically moves focus on the first element, which is something I don't want. (code in 🧵 ) Is there any way I can achieve this? This is for Compose Multiplatform (same behavior on Web + Desktop)
What I tried (which automatically selects the first element – bad):
Copy code
val focusManager = LocalFocusManager.current
    val groupFocusRequester = remember { FocusRequester() }
    Column(
        Modifier.onKeyEvent {
            focusManager.moveFocus(FocusDirection.Down)
            true
        }.focusable()
            .focusRequester(groupFocusRequester)
    ) {
        LaunchedEffect(Unit) {
            groupFocusRequester.requestFocus()
        }
        BasicText("1", Modifier.clickable { })
        BasicText("2", Modifier.clickable { })
        BasicText("3", Modifier.clickable { })
    }