English 中文(简体)
Kotlin/Wasm和键板活动
原标题:Kotlin/Wasm and keyboard events

我曾试图找到任何有关这种文件,但又找不到任何东西,我尝试采用台式堆肥方法,但没有uck,如果任何人在座,能够让我点人知道如何将其归档,我会知道。


@Composable
@Preview
fun mainScreen() {
    var xPosition by remember { mutableStateOf(50f) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color.Red)
            .onPreviewKeyEvent { event: KeyEvent ->
                if (event.type == KeyEventType.KeyDown) {
                    when (event.key) {
                        Key.DirectionLeft -> { // Left arrow key
                            xPosition -= 10f
                            true
                        }
                        Key.DirectionRight -> { // Right arrow key
                            xPosition += 10f
                            true
                        }
                        else -> false
                    }
                } else false
            },
        verticalArrangement = Arrangement.Center
    ) {
        Box(
            modifier = Modifier
                .size(100.dp)
        ) {
            Canvas(modifier = Modifier.fillMaxSize()) {
                drawRect(
                    color = Color.Blue,
                    topLeft = androidx.compose.ui.geometry.Offset(xPosition, 0f),
                    size = androidx.compose.ui.geometry.Size(100f, 100f)
                )
            }
        }
    }
}
问题回答

after a bunch of testing I was able to figure somthing out, hopefully it helps someone else :)

@Composable
fun mainScreen() {
    val squareX = remember { mutableStateOf(50.dp) }
    val squareY = remember { mutableStateOf(50.dp) }
    val squareSize = 100.dp

    keyboardEventHandler(squareX, squareY)
    movableBox(squareX.value, squareY.value, squareSize)
}

@Composable
fun movableBox(x: Dp, y: Dp, size: Dp) {
    Box(
        modifier = Modifier
            .size(size)
            .offset(x = x, y = y)
            .background(Color.Green)
    )
}

@Composable
fun keyboardEventHandler(squareX: MutableState<Dp>, squareY: MutableState<Dp>) {
    LaunchedEffect(key1 = true) {
        val handleKeyDown: (Event) -> Unit = { event ->
            (event as KeyboardEvent).let {
                when (it.key) {
                    "ArrowLeft" -> squareX.value -= 5.dp
                    "ArrowRight" -> squareX.value += 5.dp
                    "ArrowUp" -> squareY.value -= 5.dp
                    "ArrowDown" -> squareY.value += 5.dp
                }
            }
        }

        window.addEventListener("keydown", handleKeyDown)

        window.removeEventListener("keydown", handleKeyDown)
    }
}




相关问题
Exposed runs query on all HikariCP open connections

I m running a Ktor server with a PostgreSQL database and just set up the JetBrains Exposed ORM framework with HikariCP for connection pooling (per the recommendation in the Ktor documentation). My ...

SQLite Kotlin issue - no database

I am trying to create boardgamegeek APIs based app in AndroidStudio, however from unknown reason I m getting my database created with no columns. Here s Logical log: (1) no such table: games in "...

Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

热门标签