From e632060753164239cabb2956f21d9548638ff0cc Mon Sep 17 00:00:00 2001 From: Igor Demin Date: Mon, 1 Nov 2021 11:48:51 +0300 Subject: [PATCH] Update README.md (#1328) --- tutorials/Mouse_Events/README.md | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tutorials/Mouse_Events/README.md b/tutorials/Mouse_Events/README.md index 7af6e5c24b..34c78a746b 100644 --- a/tutorials/Mouse_Events/README.md +++ b/tutorials/Mouse_Events/README.md @@ -208,4 +208,39 @@ fun main() = singleWindowApplication { ``` Application running -If you need more information about events there is an available raw AWT mouse event object in `mouseEvent` property of `PointerEvent` +### Swing interoperability + +Compose for Desktop uses Swing underneath and allows to access raw AWT events: + +```kotlin +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.Text +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.* +import androidx.compose.ui.window.singleWindowApplication + +fun main() = singleWindowApplication { + var text by remember { mutableStateOf("") } + + Box( + Modifier.fillMaxSize().pointerInput(Unit) { + while (true) { + val event = awaitPointerEventScope { awaitPointerEvent() } + val awtEvent = event.mouseEvent + if (event.type == PointerEventType.Press) { + text = awtEvent?.locationOnScreen?.toString().orEmpty() + } + } + }, + contentAlignment = Alignment.Center + ) { + Text(text) + } +} +```