1- import androidx.compose.ui.ComposeScene
2- import androidx.compose.ui.ExperimentalComposeUiApi
1+ /* *
2+ * @author Enaium
3+ */
4+ import androidx.compose.ui.InternalComposeUiApi
35import androidx.compose.ui.geometry.Offset
6+ import androidx.compose.ui.input.key.Key
47import androidx.compose.ui.input.key.KeyEvent
8+ import androidx.compose.ui.input.key.KeyEventType
59import androidx.compose.ui.input.pointer.PointerEventType
10+ import androidx.compose.ui.scene.ComposeScene
611import androidx.compose.ui.unit.Density
712import org.lwjgl.glfw.GLFW.*
813import java.awt.Component
@@ -11,7 +16,7 @@ import java.awt.event.MouseEvent
1116import java.awt.event.MouseWheelEvent
1217import java.awt.event.KeyEvent as AwtKeyEvent
1318
14- @OptIn(ExperimentalComposeUiApi ::class )
19+ @OptIn(InternalComposeUiApi ::class )
1520fun ComposeScene.subscribeToGLFWEvents (windowHandle : Long ) {
1621 glfwSetMouseButtonCallback(windowHandle) { _, button, action, mods ->
1722 sendPointerEvent(
@@ -21,23 +26,23 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
2126 GLFW_RELEASE -> PointerEventType .Release
2227 else -> PointerEventType .Unknown
2328 },
24- nativeEvent = MouseEvent (getAwtMods(windowHandle))
29+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
2530 )
2631 }
2732
2833 glfwSetCursorPosCallback(windowHandle) { _, xpos, ypos ->
2934 sendPointerEvent(
3035 position = Offset (xpos.toFloat(), ypos.toFloat()),
3136 eventType = PointerEventType .Move ,
32- nativeEvent = MouseEvent (getAwtMods(windowHandle))
37+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
3338 )
3439 }
3540
3641 glfwSetCursorEnterCallback(windowHandle) { _, entered ->
3742 sendPointerEvent(
3843 position = glfwGetCursorPos(windowHandle),
3944 eventType = if (entered) PointerEventType .Enter else PointerEventType .Exit ,
40- nativeEvent = MouseEvent (getAwtMods(windowHandle))
45+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
4146 )
4247 }
4348
@@ -46,7 +51,7 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
4651 eventType = PointerEventType .Scroll ,
4752 position = glfwGetCursorPos(windowHandle),
4853 scrollDelta = Offset (xoffset.toFloat(), - yoffset.toFloat()),
49- nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
54+ nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
5055 )
5156 }
5257
@@ -62,13 +67,31 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
6267
6368 // Note that we don't distinguish between Left/Right Shift, Del from numpad or not, etc.
6469 // To distinguish we should change `location` parameter
65- sendKeyEvent(KeyEvent (awtId, time, getAwtMods(windowHandle), awtKey, 0 .toChar(), AwtKeyEvent .KEY_LOCATION_STANDARD ))
70+ sendKeyEvent(
71+ KeyEvent (
72+ awtId,
73+ time,
74+ getAwtMods(windowHandle),
75+ awtKey,
76+ 0 .toChar(),
77+ AwtKeyEvent .KEY_LOCATION_STANDARD
78+ )
79+ )
6680 }
6781
6882 glfwSetCharCallback(windowHandle) { _, codepoint ->
6983 for (char in Character .toChars(codepoint)) {
7084 val time = System .nanoTime() / 1_000_000
71- sendKeyEvent(KeyEvent (AwtKeyEvent .KEY_TYPED , time, getAwtMods(windowHandle), 0 , char, AwtKeyEvent .KEY_LOCATION_UNKNOWN ))
85+ sendKeyEvent(
86+ KeyEvent (
87+ AwtKeyEvent .KEY_TYPED ,
88+ time,
89+ getAwtMods(windowHandle),
90+ 0 ,
91+ char,
92+ AwtKeyEvent .KEY_LOCATION_UNKNOWN
93+ )
94+ )
7295 }
7396 }
7497
@@ -87,8 +110,12 @@ private fun glfwGetCursorPos(window: Long): Offset {
87110// in the future versions of Compose we plan to get rid of the need of AWT events/components
88111val awtComponent = object : Component () {}
89112
113+ @OptIn(InternalComposeUiApi ::class )
90114private fun KeyEvent (awtId : Int , time : Long , awtMods : Int , key : Int , char : Char , location : Int ) = KeyEvent (
91- AwtKeyEvent (awtComponent, awtId, time, awtMods, key, char, location)
115+ key = Key (key),
116+ codePoint = char.code,
117+ type = if (awtId == AwtKeyEvent .KEY_PRESSED ) KeyEventType .KeyDown else if (awtId == AwtKeyEvent .KEY_RELEASED ) KeyEventType .KeyUp else KeyEventType .Unknown ,
118+ nativeEvent = AwtKeyEvent (awtComponent, awtId, time, awtMods, key, char, location)
92119)
93120
94121private fun MouseEvent (awtMods : Int ) = MouseEvent (
@@ -111,11 +138,23 @@ private fun getAwtMods(windowHandle: Long): Int {
111138 awtMods = awtMods or (1 shl 14 )
112139 if (glfwGetMouseButton(windowHandle, GLFW_MOUSE_BUTTON_5 ) == GLFW_PRESS )
113140 awtMods = awtMods or (1 shl 15 )
114- if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS || glfwGetKey(windowHandle, GLFW_KEY_RIGHT_CONTROL ) == GLFW_PRESS )
141+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS || glfwGetKey(
142+ windowHandle,
143+ GLFW_KEY_RIGHT_CONTROL
144+ ) == GLFW_PRESS
145+ )
115146 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 )
147+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS || glfwGetKey(
148+ windowHandle,
149+ GLFW_KEY_RIGHT_SHIFT
150+ ) == GLFW_PRESS
151+ )
117152 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 )
153+ if (glfwGetKey(windowHandle, GLFW_KEY_LEFT_ALT ) == GLFW_PRESS || glfwGetKey(
154+ windowHandle,
155+ GLFW_KEY_RIGHT_ALT
156+ ) == GLFW_PRESS
157+ )
119158 awtMods = awtMods or InputEvent .ALT_DOWN_MASK
120159 return awtMods
121- }
160+ }
0 commit comments