Skip to content

Incorect usage of glfw key callback on unknown keys #12

@Edouard127

Description

@Edouard127

The current implementation for mapping key codes to enums is incorrect as keys not supported on the current platform will return -1 when pressed.
For a concrete example, when a user scrolls a volume knob but the keyboard doesn't have volume keys, glfw will interpret these keys as unknown

The glfw doc states:

If the specified [key token](@ref keys) corresponds to a physical key not
supported on the current platform then this method will return `-1`.

The fix is as simple as adding a Unknown enum (see glfw/glfw#1076)

(the following crash was triggered by scrolling my volume knob on my headset)
Thread backtrace:

"main@1" prio=5 tid=0x1 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
	  at uno.glfw.Key$Companion.of(_Arrays.kt:1110)
	  at uno.glfw.GlfwWindow._set_keyCB_$lambda$26$lambda$25(GlfwWindow.kt:415)
	  at uno.glfw.GlfwWindow$$Lambda/0x00000297950861f8.invoke(Unknown Source:-1)
	  at org.lwjgl.glfw.GLFWKeyCallbackI.callback(GLFWKeyCallbackI.java:44)
	  at org.lwjgl.system.JNI.callPPPPP(JNI.java:-1)
	  at org.lwjgl.system.windows.User32.nCallWindowProc(User32.java:1301)
	  at imgui.windowsIme.imeListener.invoke(imeListener.kt:179)
	  at org.lwjgl.system.windows.WindowProcI.callback(WindowProcI.java:43)
	  at org.lwjgl.system.JNI.invokeV(JNI.java:-1)
	  at org.lwjgl.glfw.GLFW.glfwPollEvents(GLFW.java:3438)
	  at uno.glfw.glfw.pollEvents(glfw.kt:260)
	  at com.lambda.ImGuiKt.main(GlfwWindow.kt:515)
	  at com.lambda.ImGuiKt.main(Unknown Source:-1)

Concerned frame:

first:1110, Key$Companion (uno.glfw)
of:141, Key$Companion (uno.glfw)
12 hidden frames
main:107, ImGuiKt (com.lambda)

this = {Key$Companion@2798} uno.glfw.Key$Companion@e67a8e0
Exception = {NoSuchElementException@2796} 
 backtrace = {Object[7]@4049} 
 detailMessage = "Array contains no element matching the predicate."
 cause = {NoSuchElementException@2796} "java.util.NoSuchElementException: Array contains no element matching the predicate."
 stackTrace = {StackTraceElement[12]@4054} 
 depth = 12
 suppressedExceptions = {Collections$EmptyList@4052}  size = 0
i = -1

glfw callback frame:

it = {ImplGlfw$Companion$keyCB$1@3147} (uno.glfw.GlfwWindow, uno.glfw.Key, kotlin.Int, uno.glfw.InputAction, kotlin.Int) -> kotlin.Unit
action = 1
key = -1
mods = 0
scanCode = 256
wnd = 2850039680800

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions