glfw: add glfw.Window.getKey

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-23 13:13:30 -07:00 committed by Stephen Gutekanst
parent 77e9396888
commit 53519d94af

View file

@ -1380,39 +1380,38 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// see also: glfw.getInputMode // /// see also: glfw.getInputMode
// GLFWAPI void glfwSetInputMode(GLFWwindow* window, int mode, int value); // GLFWAPI void glfwSetInputMode(GLFWwindow* window, int mode, int value);
// TODO(keyboard button) /// Returns the last reported press state of a keyboard key for the specified window.
// /// Returns the last reported state of a keyboard key for the specified ///
// /// window. /// This function returns the last press state reported for the specified key to the specified
// /// /// window. The returned state is one of `true` (pressed) or `false` (released). The higher-level
// /// This function returns the last state reported for the specified key to the /// action `glfw.repeat` is only reported to the key callback.
// /// specified window. The returned state is one of `glfw.press` or ///
// /// `glfw.release`. The higher-level action `glfw.repeat` is only reported to /// If the `glfw.sticky_keys` input mode is enabled, this function returns `glfw.press` the first
// /// the key callback. /// time you call it for a key that was pressed, even if that key has already been released.
// /// ///
// /// If the @ref GLFW_STICKY_KEYS input mode is enabled, this function returns /// The key functions deal with physical keys, with key tokens (see keys) named after their use on
// /// `glfw.press` the first time you call it for a key that was pressed, even if /// the standard US keyboard layout. If you want to input text, use the Unicode character callback
// /// that key has already been released. /// instead.
// /// ///
// /// The key functions deal with physical keys, with [key tokens](@ref keys) /// The modifier key bit masks (see mods) are not key tokens and cannot be used with this function.
// /// named after their use on the standard US keyboard layout. If you want to ///
// /// input text, use the Unicode character callback instead. /// __Do not use this function__ to implement text input, use glfw.Window.setCharCallback instead.
// /// ///
// /// The [modifier key bit masks](@ref mods) are not key tokens and cannot be /// @param[in] window The desired window.
// /// used with this function. /// @param[in] key The desired keyboard key (see keys). `glfw.key.unknown` is not a valid key for
// /// /// this function.
// /// __Do not use this function__ to implement [text input](@ref input_char). /// @return `true` (pressed) or `false` (released)
// /// ///
// /// @param[in] window The desired window. /// Possible errors include glfw.Error.NotInitialized and glfw.Error.InvalidEnum.
// /// @param[in] key The desired [keyboard key](@ref keys). `GLFW_KEY_UNKNOWN` is ///
// /// not a valid key for this function. /// @thread_safety This function must only be called from the main thread.
// /// @return One of `glfw.press` or `glfw.release`. ///
// /// /// see also: input_key
// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.InvalidEnum. pub inline fn getKey(self: Window, key: isize) Error!bool {
// /// const state = c.glfwGetKey(self.handle, @intCast(c_int, key));
// /// @thread_safety This function must only be called from the main thread. try getError();
// /// return state == c.GLFW_PRESS;
// /// see also: input_key }
// GLFWAPI int glfwGetKey(GLFWwindow* window, int key);
/// Returns the last reported state of a mouse button for the specified window. /// Returns the last reported state of a mouse button for the specified window.
/// ///
@ -2562,6 +2561,22 @@ test "setDropCallback" {
}).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err}); }).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err});
} }
test "getKey" {
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();
_ = try window.getKey(glfw.key.escape);
}
test "getMouseButton" { test "getMouseButton" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();
@ -2575,7 +2590,7 @@ test "getMouseButton" {
}; };
defer window.destroy(); defer window.destroy();
_ = window.getMouseButton(glfw.mouse_button.left) catch |err| std.debug.print("failed to get mouse button, not supported? error={}\n", .{err}); _ = try window.getMouseButton(glfw.mouse_button.left);
} }
test "getCursorPos" { test "getCursorPos" {