Skip to content

Commit facf550

Browse files
committed
Fix keycode translation and invalid keycode render issue
1 parent 348e408 commit facf550

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/main/kotlin/com/lambda/config/settings/complex/KeybindSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class KeybindSettingCore(defaultValue: Bind) : SettingCore<Bind>(
113113
when (it.translated) {
114114
KeyCode.Escape -> {}
115115
KeyCode.Backspace, KeyCode.Delete -> value = Bind.EMPTY
116-
else -> value = Bind(it.keyCode, it.modifiers, -1)
116+
else -> value = Bind(it.translated.code, it.modifiers, -1)
117117
}
118118

119119
listening = false

src/main/kotlin/com/lambda/event/events/KeyboardEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ sealed class KeyboardEvent {
4141
val modifiers: Int,
4242
) : Event {
4343
val bind: Bind
44-
get() = Bind(keyCode, modifiers, -1)
44+
get() = Bind(translated.code, modifiers, -1)
4545

4646
val translated: KeyCode
4747
get() = KeyCode.virtualMapUS(keyCode, scanCode)
4848

4949
val isPressed = action >= GLFW_PRESS
5050
val isReleased = action == GLFW_RELEASE
5151

52-
fun satisfies(bind: Bind) = bind.key == keyCode && bind.modifiers and modifiers == bind.modifiers
52+
fun satisfies(bind: Bind) = bind.key == translated.code && bind.modifiers and modifiers == bind.modifiers
5353
}
5454

5555
/**

src/main/kotlin/com/lambda/util/InputUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ object InputUtils : Loadable {
7373
val key = pressedKeys
7474
.firstNotNullOfOrNull { (key, state) -> key to state } ?: return null
7575

76-
val scancode = scancodes.getValue(key.first)
76+
val scancode = scancodes.getOrElse(key.first) { 0 }
7777

7878
return KeyboardEvent.Press(key.first, scancode, key.second, mods)
7979
}
@@ -93,7 +93,7 @@ object InputUtils : Loadable {
9393
return MouseEvent.Click(mouse, GLFW_PRESS, mods)
9494
}
9595

96-
private val keys = KeyCode.entries.map { it.code }
96+
private val keys = KeyCode.entries.map { it.code }.filter { it > 0 }
9797
private val scancodes = keys.associateWith { GLFW.glfwGetKeyScancode(it) }
9898

9999
private val mouses = GLFW_MOUSE_BUTTON_1..GLFW_MOUSE_BUTTON_8

src/main/kotlin/com/lambda/util/KeyCode.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ enum class KeyCode(val code: Int) {
144144
Last(GLFW.GLFW_KEY_LAST);
145145

146146
companion object {
147-
private const val printablePool = "`-=[]\\,;\'./"
147+
private const val PRINTABLE_POOL = "`-=[]\\,;\'./"
148148
private val glfwPool = intArrayOf(
149149
GLFW.GLFW_KEY_GRAVE_ACCENT, GLFW.GLFW_KEY_MINUS, GLFW.GLFW_KEY_EQUAL,
150150
GLFW.GLFW_KEY_LEFT_BRACKET, GLFW.GLFW_KEY_RIGHT_BRACKET, GLFW.GLFW_KEY_BACKSLASH,
@@ -168,6 +168,7 @@ enum class KeyCode(val code: Int) {
168168
* @see <a href="https://github.com/glfw/glfw/issues/1502#issuecomment-1005841055">ImGui impl
169169
*/
170170
fun virtualMapUS(keyCode: Int, scanCode: Int): KeyCode {
171+
if (keyCode <= 0) return fromKeyCode(keyCode)
171172
if (keyCode in GLFW.GLFW_KEY_KP_0..GLFW.GLFW_KEY_KP_EQUAL) return fromKeyCode(keyCode)
172173

173174
val keyName = GLFW.glfwGetKeyName(keyCode, scanCode) ?: return fromKeyCode(keyCode)
@@ -179,8 +180,8 @@ enum class KeyCode(val code: Int) {
179180
in 'A'..'Z' -> GLFW.GLFW_KEY_A + (char - 'A')
180181
in 'a'..'z' -> GLFW.GLFW_KEY_A + (char - 'a')
181182
else -> {
182-
val i = printablePool.indexOf(keyName)
183-
if (i > 0) glfwPool[i] else keyCode
183+
val i = PRINTABLE_POOL.indexOf(keyName)
184+
if (i >= 0) glfwPool[i] else keyCode
184185
}
185186
})
186187
}

0 commit comments

Comments
 (0)