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