@@ -39,7 +39,14 @@ import imgui.flag.ImGuiCol
3939import imgui.flag.ImGuiHoveredFlags
4040import imgui.flag.ImGuiMouseButton
4141import net.minecraft.command.CommandRegistryAccess
42- import org.lwjgl.glfw.GLFW
42+ import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT
43+ import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SUPER
44+ import org.lwjgl.glfw.GLFW.GLFW_MOD_ALT
45+ import org.lwjgl.glfw.GLFW.GLFW_MOD_CAPS_LOCK
46+ import org.lwjgl.glfw.GLFW.GLFW_MOD_CONTROL
47+ import org.lwjgl.glfw.GLFW.GLFW_MOD_NUM_LOCK
48+ import org.lwjgl.glfw.GLFW.GLFW_MOD_SHIFT
49+ import org.lwjgl.glfw.GLFW.GLFW_MOD_SUPER
4350
4451class KeybindSetting (
4552 override val name : String ,
@@ -56,10 +63,9 @@ class KeybindSetting(
5663
5764 override fun ImGuiBuilder.buildLayout () {
5865 val bind = value
59- val scancode = if (bind.code == KeyCode .UNBOUND .code) - 69 else GLFW .glfwGetKeyScancode(bind.code)
60- val translated = if (bind.keyCode == KeyCode .UNBOUND ) bind.keyCode else KeyCode .virtualMapUS(bind.code, scancode)
61- val preview = if (listening) " $name : Press any key…"
62- else if (bind.isMouseButton) " Mouse ${bind.code} " else " $name : $translated "
66+ val preview =
67+ if (listening) " $name : Press any key…"
68+ else bind.name
6369
6470 if (listening) {
6571 withStyleColor(ImGuiCol .Button , 0.20f , 0.50f , 1.00f , 1.00f ) {
@@ -99,18 +105,23 @@ class KeybindSetting(
99105 lambdaTooltip(" Clear binding" )
100106 }
101107
102- val keyboardPoll = InputUtils .lastKeyboardEvent
103- val mousePoll = InputUtils .lastMouseEvent
108+ val keypoll = InputUtils .lastKeyboardEvent ? : return
109+ val mousepoll = InputUtils .lastMouseEvent ? : return
110+
104111 if (listening) {
105- if (keyboardPoll.isPressed) {
106- when (val key = keyboardPoll.translated) {
112+ val isModKey = keypoll.keyCode in GLFW_KEY_LEFT_SHIFT .. GLFW_KEY_RIGHT_SUPER
113+
114+ if ((keypoll.isPressed && ! isModKey)
115+ || (keypoll.isReleased && isModKey)
116+ ) {
117+ when (keypoll.translated) {
107118 KeyCode .ESCAPE -> {}
108119 KeyCode .BACKSPACE , KeyCode .DELETE -> value = Bind .EMPTY
109- else -> value = Bind (key )
120+ else -> value = Bind (keypoll.keyCode, keypoll.modifiers, - 1 )
110121 }
111122 listening = false
112- } else if (mousePoll .action == Mouse .Action .Click .ordinal) {
113- value = mouseBind(mousePoll .button)
123+ } else if (mousepoll .action == Mouse .Action .Click .ordinal) {
124+ value = Bind ( 0 , mousepoll.modifiers, mousepoll .button)
114125 listening = false
115126 }
116127 }
@@ -136,7 +147,7 @@ class KeybindSetting(
136147 bind = mouseBind(num)
137148 } else {
138149 bind = try {
139- Bind (KeyCode .valueOf(name().value()))
150+ Bind (KeyCode .valueOf(name().value()).code, 0 )
140151 } catch (_: IllegalArgumentException ) {
141152 return @executeWithResult failure(" ${name().value()} doesn't match with a bind" )
142153 }
@@ -151,19 +162,34 @@ class KeybindSetting(
151162}
152163
153164data class Bind (
154- val keyCode : KeyCode = KeyCode . UNBOUND ,
155- val code : Int = keyCode.code ,
156- val isMouseButton : Boolean = false
165+ val key : Int ,
166+ val modifiers : Int ,
167+ val mouse : Int = - 1 ,
157168) {
169+ val truemods = buildList {
170+ if (modifiers and GLFW_MOD_SHIFT != 0 ) add(KeyCode .LEFT_SHIFT )
171+ if (modifiers and GLFW_MOD_CONTROL != 0 ) add(KeyCode .LEFT_CONTROL )
172+ if (modifiers and GLFW_MOD_ALT != 0 ) add(KeyCode .LEFT_ALT )
173+ if (modifiers and GLFW_MOD_SUPER != 0 ) add(KeyCode .LEFT_SUPER )
174+ if (modifiers and GLFW_MOD_CAPS_LOCK != 0 ) add(KeyCode .CAPS_LOCK )
175+ if (modifiers and GLFW_MOD_NUM_LOCK != 0 ) add(KeyCode .NUM_LOCK )
176+ }
177+
158178 val name: String
159- get() = if (isMouseButton) " Mouse $code " else keyCode.name
179+ get() = buildString {
180+ if (mouse >= 0 ) append(" ${Mouse .Button .fromMouseCode(mouse)} + " )
181+ if (modifiers > 0 ) append(" ${truemods.joinToString(separator = " +" ) { it.name }} +" )
182+ if (key > 0 ) append(KeyCode .fromKeyCode(key))
183+
184+ if (mouse < 0 && modifiers <= 0 && key <= 0 ) append(" Unbound" )
185+ }
160186
161187 override fun toString () =
162- " Key Code: $keyCode , Code: $code , Mouse Button: $isMouseButton "
188+ " Key Code: $key "
163189
164190 companion object {
165- val EMPTY = Bind ()
191+ val EMPTY = Bind (0 , 0 )
166192
167- fun mouseBind (code : Int ) = Bind (code = code, isMouseButton = true )
193+ fun mouseBind (code : Int ) = Bind (0 , 0 , code )
168194 }
169195}
0 commit comments