1- import androidx.compose.ui.ComposeScene
2- import androidx.compose.ui.ExperimentalComposeUiApi
1+ import androidx.compose.ui.InternalComposeUiApi
32import androidx.compose.ui.geometry.Offset
3+ import androidx.compose.ui.input.key.Key
44import androidx.compose.ui.input.key.KeyEvent
5+ import androidx.compose.ui.input.key.KeyEventType
56import androidx.compose.ui.input.pointer.PointerEventType
7+ import androidx.compose.ui.scene.ComposeScene
68import androidx.compose.ui.unit.Density
79import org.lwjgl.glfw.GLFW.*
810import java.awt.Component
@@ -11,7 +13,7 @@ import java.awt.event.MouseEvent
1113import java.awt.event.MouseWheelEvent
1214import java.awt.event.KeyEvent as AwtKeyEvent
1315
14- @OptIn(ExperimentalComposeUiApi ::class )
16+ @OptIn(InternalComposeUiApi ::class )
1517fun ComposeScene.subscribeToGLFWEvents (windowHandle : Long ) {
1618 glfwSetMouseButtonCallback(windowHandle) { _, button, action, mods ->
1719 sendPointerEvent(
@@ -21,23 +23,23 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
2123 GLFW_RELEASE -> PointerEventType .Release
2224 else -> PointerEventType .Unknown
2325 },
24- nativeEvent = MouseEvent (getAwtMods(windowHandle))
26+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
2527 )
2628 }
2729
2830 glfwSetCursorPosCallback(windowHandle) { _, xpos, ypos ->
2931 sendPointerEvent(
3032 position = Offset (xpos.toFloat(), ypos.toFloat()),
3133 eventType = PointerEventType .Move ,
32- nativeEvent = MouseEvent (getAwtMods(windowHandle))
34+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
3335 )
3436 }
3537
3638 glfwSetCursorEnterCallback(windowHandle) { _, entered ->
3739 sendPointerEvent(
3840 position = glfwGetCursorPos(windowHandle),
3941 eventType = if (entered) PointerEventType .Enter else PointerEventType .Exit ,
40- nativeEvent = MouseEvent (getAwtMods(windowHandle))
42+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
4143 )
4244 }
4345
@@ -46,7 +48,7 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
4648 eventType = PointerEventType .Scroll ,
4749 position = glfwGetCursorPos(windowHandle),
4850 scrollDelta = Offset (xoffset.toFloat(), - yoffset.toFloat()),
49- nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
51+ nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
5052 )
5153 }
5254
@@ -62,13 +64,31 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
6264
6365 // Note that we don't distinguish between Left/Right Shift, Del from numpad or not, etc.
6466 // To distinguish we should change `location` parameter
65- sendKeyEvent(KeyEvent (awtId, time, getAwtMods(windowHandle), awtKey, 0 .toChar(), AwtKeyEvent .KEY_LOCATION_STANDARD ))
67+ sendKeyEvent(
68+ KeyEvent (
69+ awtId,
70+ time,
71+ getAwtMods(windowHandle),
72+ awtKey,
73+ 0 .toChar(),
74+ AwtKeyEvent .KEY_LOCATION_STANDARD
75+ )
76+ )
6677 }
6778
6879 glfwSetCharCallback(windowHandle) { _, codepoint ->
6980 for (char in Character .toChars(codepoint)) {
7081 val time = System .nanoTime() / 1_000_000
71- sendKeyEvent(KeyEvent (AwtKeyEvent .KEY_TYPED , time, getAwtMods(windowHandle), 0 , char, AwtKeyEvent .KEY_LOCATION_UNKNOWN ))
82+ sendKeyEvent(
83+ KeyEvent (
84+ AwtKeyEvent .KEY_TYPED ,
85+ time,
86+ getAwtMods(windowHandle),
87+ 0 ,
88+ char,
89+ AwtKeyEvent .KEY_LOCATION_UNKNOWN
90+ )
91+ )
7292 }
7393 }
7494
@@ -87,8 +107,12 @@ private fun glfwGetCursorPos(window: Long): Offset {
87107// in the future versions of Compose we plan to get rid of the need of AWT events/components
88108val awtComponent = object : Component () {}
89109
110+ @OptIn(InternalComposeUiApi ::class )
90111private fun KeyEvent (awtId : Int , time : Long , awtMods : Int , key : Int , char : Char , location : Int ) = KeyEvent (
91- AwtKeyEvent (awtComponent, awtId, time, awtMods, key, char, location)
112+ key = Key (key),
113+ codePoint = char.code,
114+ type = if (awtId == AwtKeyEvent .KEY_PRESSED ) KeyEventType .KeyDown else if (awtId == AwtKeyEvent .KEY_RELEASED ) KeyEventType .KeyUp else KeyEventType .Unknown ,
115+ nativeEvent = AwtKeyEvent (awtComponent, awtId, time, awtMods, key, char, location)
92116)
93117
94118private fun MouseEvent (awtMods : Int ) = MouseEvent (
@@ -111,11 +135,23 @@ private fun getAwtMods(windowHandle: Long): Int {
111135 awtMods = awtMods or (1 shl 14 )
112136 if (glfwGetMouseButton(windowHandle, GLFW_MOUSE_BUTTON_5 ) == GLFW_PRESS )
113137 awtMods = awtMods or (1 shl 15 )
114- if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS || glfwGetKey(windowHandle, GLFW_KEY_RIGHT_CONTROL ) == GLFW_PRESS )
138+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS || glfwGetKey(
139+ windowHandle,
140+ GLFW_KEY_RIGHT_CONTROL
141+ ) == GLFW_PRESS
142+ )
115143 awtMods = awtMods or InputEvent .CTRL_DOWN_MASK
116- if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS || glfwGetKey(windowHandle, GLFW_KEY_RIGHT_SHIFT ) == GLFW_PRESS )
144+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS || glfwGetKey(
145+ windowHandle,
146+ GLFW_KEY_RIGHT_SHIFT
147+ ) == GLFW_PRESS
148+ )
117149 awtMods = awtMods or InputEvent .SHIFT_DOWN_MASK
118- if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_ALT ) == GLFW_PRESS || glfwGetKey(windowHandle, GLFW_KEY_RIGHT_ALT ) == GLFW_PRESS )
150+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_ALT ) == GLFW_PRESS || glfwGetKey(
151+ windowHandle,
152+ GLFW_KEY_RIGHT_ALT
153+ ) == GLFW_PRESS
154+ )
119155 awtMods = awtMods or InputEvent .ALT_DOWN_MASK
120156 return awtMods
121- }
157+ }
0 commit comments