1
- import androidx.compose.ui.ComposeScene
2
- import androidx.compose.ui.ExperimentalComposeUiApi
1
+ /* *
2
+ * @author Enaium
3
+ */
4
+ import androidx.compose.ui.InternalComposeUiApi
3
5
import androidx.compose.ui.geometry.Offset
6
+ import androidx.compose.ui.input.key.Key
4
7
import androidx.compose.ui.input.key.KeyEvent
8
+ import androidx.compose.ui.input.key.KeyEventType
5
9
import androidx.compose.ui.input.pointer.PointerEventType
10
+ import androidx.compose.ui.scene.ComposeScene
6
11
import androidx.compose.ui.unit.Density
7
12
import org.lwjgl.glfw.GLFW.*
8
13
import java.awt.Component
@@ -11,7 +16,7 @@ import java.awt.event.MouseEvent
11
16
import java.awt.event.MouseWheelEvent
12
17
import java.awt.event.KeyEvent as AwtKeyEvent
13
18
14
- @OptIn(ExperimentalComposeUiApi ::class )
19
+ @OptIn(InternalComposeUiApi ::class )
15
20
fun ComposeScene.subscribeToGLFWEvents (windowHandle : Long ) {
16
21
glfwSetMouseButtonCallback(windowHandle) { _, button, action, mods ->
17
22
sendPointerEvent(
@@ -21,23 +26,23 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
21
26
GLFW_RELEASE -> PointerEventType .Release
22
27
else -> PointerEventType .Unknown
23
28
},
24
- nativeEvent = MouseEvent (getAwtMods(windowHandle))
29
+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
25
30
)
26
31
}
27
32
28
33
glfwSetCursorPosCallback(windowHandle) { _, xpos, ypos ->
29
34
sendPointerEvent(
30
35
position = Offset (xpos.toFloat(), ypos.toFloat()),
31
36
eventType = PointerEventType .Move ,
32
- nativeEvent = MouseEvent (getAwtMods(windowHandle))
37
+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
33
38
)
34
39
}
35
40
36
41
glfwSetCursorEnterCallback(windowHandle) { _, entered ->
37
42
sendPointerEvent(
38
43
position = glfwGetCursorPos(windowHandle),
39
44
eventType = if (entered) PointerEventType .Enter else PointerEventType .Exit ,
40
- nativeEvent = MouseEvent (getAwtMods(windowHandle))
45
+ nativeEvent = MouseEvent (getAwtMods(windowHandle))
41
46
)
42
47
}
43
48
@@ -46,7 +51,7 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
46
51
eventType = PointerEventType .Scroll ,
47
52
position = glfwGetCursorPos(windowHandle),
48
53
scrollDelta = Offset (xoffset.toFloat(), - yoffset.toFloat()),
49
- nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
54
+ nativeEvent = MouseWheelEvent (getAwtMods(windowHandle))
50
55
)
51
56
}
52
57
@@ -62,13 +67,31 @@ fun ComposeScene.subscribeToGLFWEvents(windowHandle: Long) {
62
67
63
68
// Note that we don't distinguish between Left/Right Shift, Del from numpad or not, etc.
64
69
// 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
+ )
66
80
}
67
81
68
82
glfwSetCharCallback(windowHandle) { _, codepoint ->
69
83
for (char in Character .toChars(codepoint)) {
70
84
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
+ )
72
95
}
73
96
}
74
97
@@ -87,8 +110,12 @@ private fun glfwGetCursorPos(window: Long): Offset {
87
110
// in the future versions of Compose we plan to get rid of the need of AWT events/components
88
111
val awtComponent = object : Component () {}
89
112
113
+ @OptIn(InternalComposeUiApi ::class )
90
114
private 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)
92
119
)
93
120
94
121
private fun MouseEvent (awtMods : Int ) = MouseEvent (
@@ -111,11 +138,23 @@ private fun getAwtMods(windowHandle: Long): Int {
111
138
awtMods = awtMods or (1 shl 14 )
112
139
if (glfwGetMouseButton(windowHandle, GLFW_MOUSE_BUTTON_5 ) == GLFW_PRESS )
113
140
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
+ )
115
146
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
+ )
117
152
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
+ )
119
158
awtMods = awtMods or InputEvent .ALT_DOWN_MASK
120
159
return awtMods
121
- }
160
+ }
0 commit comments