glfw: add glfw.Window.setKeyCallback
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
041dad6586
commit
888cd5ade6
1 changed files with 80 additions and 99 deletions
|
|
@ -45,12 +45,13 @@ pub const InternalUserPointer = struct {
|
||||||
setMaximizeCallback: ?fn (window: Window, maximized: bool) void,
|
setMaximizeCallback: ?fn (window: Window, maximized: bool) void,
|
||||||
setFramebufferSizeCallback: ?fn (window: Window, width: isize, height: isize) void,
|
setFramebufferSizeCallback: ?fn (window: Window, width: isize, height: isize) void,
|
||||||
setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void,
|
setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void,
|
||||||
setDropCallback: ?fn (window: Window, paths: [][*c]const u8) void,
|
setKeyCallback: ?fn (window: Window, key: isize, scancode: isize, action: isize, mods: isize) void,
|
||||||
setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void,
|
|
||||||
setCursorEnterCallback: ?fn (window: Window, entered: bool) void,
|
|
||||||
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
|
|
||||||
setMouseButtonCallback: ?fn (window: Window, button: isize, action: isize, mods: isize) void,
|
|
||||||
setCharCallback: ?fn (window: Window, codepoint: u21) void,
|
setCharCallback: ?fn (window: Window, codepoint: u21) void,
|
||||||
|
setMouseButtonCallback: ?fn (window: Window, button: isize, action: isize, mods: isize) void,
|
||||||
|
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
|
||||||
|
setCursorEnterCallback: ?fn (window: Window, entered: bool) void,
|
||||||
|
setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void,
|
||||||
|
setDropCallback: ?fn (window: Window, paths: [][*c]const u8) void,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Resets all window hints to their default values.
|
/// Resets all window hints to their default values.
|
||||||
|
|
@ -1301,29 +1302,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
getError() catch {};
|
getError() catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// /// The function pointer type for keyboard key callbacks.
|
|
||||||
// ///
|
|
||||||
// /// This is the function pointer type for keyboard key callbacks. A keyboard
|
|
||||||
// /// key callback function has the following signature:
|
|
||||||
// /// @code
|
|
||||||
// /// void function_name(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
||||||
// /// @endcode
|
|
||||||
// ///
|
|
||||||
// /// @param[in] window The window that received the event.
|
|
||||||
// /// @param[in] key The [keyboard key](@ref keys) that was pressed or released.
|
|
||||||
// /// @param[in] scancode The system-specific scancode of the key.
|
|
||||||
// /// @param[in] action `glfw.press`, `glfw.release` or `glfw.repeat`. Future
|
|
||||||
// /// releases may add more actions.
|
|
||||||
// /// @param[in] mods Bit field describing which [modifier keys](@ref mods) were
|
|
||||||
// /// held down.
|
|
||||||
// ///
|
|
||||||
// /// see also: input_key, glfwSetKeyCallback
|
|
||||||
// ///
|
|
||||||
// /// @glfw3 Added window handle, scancode and modifier mask parameters.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int);
|
|
||||||
|
|
||||||
// TODO(mouinput options)
|
// TODO(mouinput options)
|
||||||
// /// Returns the value of an input option for the specified window.
|
// /// Returns the value of an input option for the specified window.
|
||||||
// ///
|
// ///
|
||||||
|
|
@ -1342,9 +1320,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: glfw.setInputMode
|
// /// see also: glfw.setInputMode
|
||||||
// ///
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI int glfwGetInputMode(GLFWwindow* window, int mode);
|
// GLFWAPI int glfwGetInputMode(GLFWwindow* window, int mode);
|
||||||
|
|
||||||
// TODO(mouinput options)
|
// TODO(mouinput options)
|
||||||
|
|
@ -1402,9 +1377,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: glfw.getInputMode
|
// /// see also: glfw.getInputMode
|
||||||
// /// Replaces `glfwEnable` and `glfwDisable`.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI void glfwSetInputMode(GLFWwindow* window, int mode, int value);
|
// GLFWAPI void glfwSetInputMode(GLFWwindow* window, int mode, int value);
|
||||||
|
|
||||||
// TODO(keyboard button)
|
// TODO(keyboard button)
|
||||||
|
|
@ -1439,10 +1411,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: input_key
|
// /// see also: input_key
|
||||||
// ///
|
|
||||||
// /// @glfw3 Added window handle parameter.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI int glfwGetKey(GLFWwindow* window, int key);
|
// GLFWAPI int glfwGetKey(GLFWwindow* window, int key);
|
||||||
|
|
||||||
// TODO(mouse button)
|
// TODO(mouse button)
|
||||||
|
|
@ -1466,10 +1434,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: input_mouse_button
|
// /// see also: input_mouse_button
|
||||||
// ///
|
|
||||||
// /// @glfw3 Added window handle parameter.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI int glfwGetMouseButton(GLFWwindow* window, int button);
|
// GLFWAPI int glfwGetMouseButton(GLFWwindow* window, int button);
|
||||||
|
|
||||||
// TODO(cursor position)
|
// TODO(cursor position)
|
||||||
|
|
@ -1502,9 +1466,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: cursor_pos, glfw.setCursorPos
|
// /// see also: cursor_pos, glfw.setCursorPos
|
||||||
// /// Replaces `glfwGetMousePos`.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI void glfwGetCursorPos(GLFWwindow* window, double* xpos, double* ypos);
|
// GLFWAPI void glfwGetCursorPos(GLFWwindow* window, double* xpos, double* ypos);
|
||||||
|
|
||||||
// TODO(cursor position)
|
// TODO(cursor position)
|
||||||
|
|
@ -1539,9 +1500,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: cursor_pos, glfw.getCursorPos
|
// /// see also: cursor_pos, glfw.getCursorPos
|
||||||
// /// Replaces `glfwSetMousePos`.
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos);
|
// GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos);
|
||||||
|
|
||||||
// TODO(cursor icon)
|
// TODO(cursor icon)
|
||||||
|
|
@ -1564,59 +1522,58 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
// /// @thread_safety This function must only be called from the main thread.
|
||||||
// ///
|
// ///
|
||||||
// /// see also: cursor_object
|
// /// see also: cursor_object
|
||||||
// ///
|
|
||||||
// ///
|
|
||||||
// /// @ingroup input
|
|
||||||
// GLFWAPI void glfwSetCursor(GLFWwindow* window, GLFWcursor* cursor);
|
// GLFWAPI void glfwSetCursor(GLFWwindow* window, GLFWcursor* cursor);
|
||||||
|
|
||||||
// TODO(key button)
|
fn setKeyCallbackWrapper(handle: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void {
|
||||||
// /// Sets the key callback.
|
const window = from(handle.?) catch unreachable;
|
||||||
// ///
|
const internal = window.getInternal();
|
||||||
// /// This function sets the key callback of the specified window, which is called
|
internal.setKeyCallback.?(window, @intCast(isize, key), @intCast(isize, scancode), @intCast(isize, action), @intCast(isize, mods));
|
||||||
// /// when a key is pressed, repeated or released.
|
}
|
||||||
// ///
|
|
||||||
// /// The key functions deal with physical keys, with layout independent
|
/// Sets the key callback.
|
||||||
// /// [key tokens](@ref keys) named after their values in the standard US keyboard
|
///
|
||||||
// /// layout. If you want to input text, use the
|
/// This function sets the key callback of the specified window, which is called when a key is
|
||||||
// /// [character callback](@ref glfwSetCharCallback) instead.
|
/// pressed, repeated or released.
|
||||||
// ///
|
///
|
||||||
// /// When a window loses input focus, it will generate synthetic key release
|
/// The key functions deal with physical keys, with layout independent key tokens (see keys) named
|
||||||
// /// events for all pressed keys. You can tell these events from user-generated
|
/// after their values in the standard US keyboard layout. If you want to input text, use the
|
||||||
// /// events by the fact that the synthetic ones are generated after the focus
|
/// character callback (see glfw.Window.setCharCallback) instead.
|
||||||
// /// loss event has been processed, i.e. after the
|
///
|
||||||
// /// [window focus callback](@ref glfwSetWindowFocusCallback) has been called.
|
/// When a window loses input focus, it will generate synthetic key release events for all pressed
|
||||||
// ///
|
/// keys. You can tell these events from user-generated events by the fact that the synthetic ones
|
||||||
// /// The scancode of a key is specific to that platform or sometimes even to that
|
/// are generated after the focus loss event has been processed, i.e. after the window focus
|
||||||
// /// machine. Scancodes are intended to allow users to bind keys that don't have
|
/// callback (see glfw.Window.setFocusCallback) has been called.
|
||||||
// /// a GLFW key token. Such keys have `key` set to `GLFW_KEY_UNKNOWN`, their
|
///
|
||||||
// /// state is not saved and so it cannot be queried with @ref glfwGetKey.
|
/// The scancode of a key is specific to that platform or sometimes even to that machine. Scancodes
|
||||||
// ///
|
/// are intended to allow users to bind keys that don't have a GLFW key token. Such keys have `key`
|
||||||
// /// Sometimes GLFW needs to generate synthetic key events, in which case the
|
/// set to `glfw.key.unknown`, their state is not saved and so it cannot be queried with
|
||||||
// /// scancode may be zero.
|
/// glfw.Window.getKey.
|
||||||
// ///
|
///
|
||||||
// /// @param[in] window The window whose callback to set.
|
/// Sometimes GLFW needs to generate synthetic key events, in which case the scancode may be zero.
|
||||||
// /// @param[in] callback The new key callback, or null to remove the currently
|
///
|
||||||
// /// set callback.
|
/// @param[in] window The window whose callback to set.
|
||||||
// /// @return The previously set callback, or null if no callback was set or the
|
/// @param[in] callback The new key callback, or null to remove the currently set callback.
|
||||||
// /// library had not been [initialized](@ref intro_init).
|
///
|
||||||
// ///
|
/// @callback_param[in] window The window that received the event.
|
||||||
// /// @callback_signature
|
/// @callback_param[in] key The keyboard key (see keys) that was pressed or released.
|
||||||
// /// @code
|
/// @callback_param[in] scancode The system-specific scancode of the key.
|
||||||
// /// void function_name(GLFWwindow* window, int key, int scancode, int action, int mods)
|
/// @callback_param[in] action `glfw.press`, `glfw.release` or `glfw.repeat`. Future releases may
|
||||||
// /// @endcode
|
/// add more actions.
|
||||||
// /// For more information about the callback parameters, see the
|
/// @callback_param[in] mods Bit field describing which modifier keys (see mods) were held down.
|
||||||
// /// [function pointer type](@ref GLFWkeyfun).
|
///
|
||||||
// ///
|
/// @thread_safety This function must only be called from the main thread.
|
||||||
// /// Possible errors include glfw.Error.NotInitialized.
|
///
|
||||||
// ///
|
/// see also: input_key
|
||||||
// /// @thread_safety This function must only be called from the main thread.
|
pub inline fn setKeyCallback(self: Window, callback: ?fn (window: Window, key: isize, scancode: isize, action: isize, mods: isize) void) void {
|
||||||
// ///
|
var internal = self.getInternal();
|
||||||
// /// see also: input_key
|
internal.setKeyCallback = callback;
|
||||||
// ///
|
_ = c.glfwSetKeyCallback(self.handle, if (callback != null) setKeyCallbackWrapper else null);
|
||||||
// /// @glfw3 Added window handle parameter and return value.
|
|
||||||
// ///
|
// The only error this could return would be glfw.Error.NotInitialized, which should
|
||||||
// /// @ingroup input
|
// definitely have occurred before calls to this. Returning an error here makes the API
|
||||||
// GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun callback);
|
// awkward to use, so we discard it instead.
|
||||||
|
getError() catch {};
|
||||||
|
}
|
||||||
|
|
||||||
fn setCharCallbackWrapper(handle: ?*c.GLFWwindow, codepoint: c_uint) callconv(.C) void {
|
fn setCharCallbackWrapper(handle: ?*c.GLFWwindow, codepoint: c_uint) callconv(.C) void {
|
||||||
const window = from(handle.?) catch unreachable;
|
const window = from(handle.?) catch unreachable;
|
||||||
|
|
@ -2714,3 +2671,27 @@ test "setCharCallback" {
|
||||||
}
|
}
|
||||||
}).callback);
|
}).callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "setKeyCallback" {
|
||||||
|
const glfw = @import("main.zig");
|
||||||
|
try glfw.init();
|
||||||
|
defer glfw.terminate();
|
||||||
|
|
||||||
|
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null) catch |err| {
|
||||||
|
// return without fail, because most of our CI environments are headless / we cannot open
|
||||||
|
// windows on them.
|
||||||
|
std.debug.print("note: failed to create window: {}\n", .{err});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
defer window.destroy();
|
||||||
|
|
||||||
|
window.setKeyCallback((struct {
|
||||||
|
fn callback(_window: Window, key: isize, scancode: isize, action: isize, mods: isize) void {
|
||||||
|
_ = _window;
|
||||||
|
_ = key;
|
||||||
|
_ = scancode;
|
||||||
|
_ = action;
|
||||||
|
_ = mods;
|
||||||
|
}
|
||||||
|
}).callback);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue