Skip to content

Commit 6d1e509

Browse files
committed
Handle null values from GetKeyName
1 parent 0a23296 commit 6d1e509

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

examples/callbacks.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import GLFW
22

3-
window = GLFW.CreateWindow(800, 600, "GLFW Callback Test")
3+
window = GLFW.CreateWindow(640, 480, "GLFW Callback Test")
44
GLFW.MakeContextCurrent(window)
55

66
# Window callbacks
@@ -17,12 +17,8 @@ actiontext = Dict(
1717
GLFW.REPEAT => "repeats"
1818
)
1919
GLFW.SetKeyCallback(window, (_, key, scancode, action, mods) -> begin
20-
char = convert(Char, key)
21-
if '!' <= char && char <= '~'
22-
println("key '$char' ", actiontext[action])
23-
else
24-
println("key $key ", actiontext[action])
25-
end
20+
name = get(GLFW.GetKeyName(key, scancode), "$key")
21+
println("key $name ", actiontext[action])
2622
end)
2723

2824
GLFW.SetCharModsCallback(window, (_, c, mods) -> println("char: $c, mods: $mods"))

src/glfw3.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,16 @@ end
415415

416416
SetInputMode(window::Window, mode::Integer, value::Integer) = ccall( (:glfwSetInputMode, lib), Void, (WindowHandle, Cint, Cint), window, mode, value)
417417
GetKey(window::Window, key::Integer) = Bool(ccall( (:glfwGetKey, lib), Cint, (WindowHandle, Cint), window, key))
418-
GetKeyName(key::Integer, scancode::Integer) = unsafe_string(ccall( (:glfwGetKeyName, lib), Cstring, (Cint, Cint), key, scancode))
418+
419+
function GetKeyName(key::Integer, scancode::Integer)
420+
ptr = ccall( (:glfwGetKeyName, lib), Cstring, (Cint, Cint), key, scancode)
421+
if ptr == C_NULL
422+
Nullable{String}()
423+
else
424+
Nullable(unsafe_string(ptr))
425+
end
426+
end
427+
419428
GetMouseButton(window::Window, button::Integer) = Bool(ccall( (:glfwGetMouseButton, lib), Cint, (WindowHandle, Cint), window, button))
420429

421430
function GetCursorPos(window::Window)

0 commit comments

Comments
 (0)