glfw: add glfw.key.getName

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-23 10:30:40 -07:00 committed by Stephen Gutekanst
parent 6e3687e3aa
commit 3e466da662
2 changed files with 83 additions and 72 deletions

View file

@ -8,14 +8,17 @@
//! The naming of the key codes follow these rules: //! The naming of the key codes follow these rules:
//! //!
//! - The US keyboard layout is used //! - The US keyboard layout is used
//! - Names of printable alpha-numeric characters are used (e.g. "A", "R", "3", etc.) //! - Names of printable alpha-numeric characters are used (e.g. "a", "r", "three", etc.)
//! - For non-alphanumeric characters, Unicode:ish names are used (e.g. "COMMA", //! - For non-alphanumeric characters, Unicode:ish names are used (e.g. "comma", "left_bracket",
//! "LEFT_SQUARE_BRACKET", etc.). Note that some names do not correspond to the Unicode standard //! etc.). Note that some names do not correspond to the Unicode standard (usually for brevity)
//! (usually for brevity) //! - Keys that lack a clear US mapping are named "world_x"
//! - Keys that lack a clear US mapping are named "WORLD_x" //! - For non-printable keys, custom names are used (e.g. "F4", "backspace", etc.)
//! - For non-printable keys, custom names are used (e.g. "F4", "BACKSPACE", etc.)
const std = @import("std");
const cc = @import("c.zig").c; const cc = @import("c.zig").c;
const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError;
/// The unknown key /// The unknown key
pub const unknown = cc.GLFW_KEY_UNKNOWN; pub const unknown = cc.GLFW_KEY_UNKNOWN;
@ -146,78 +149,77 @@ pub const menu = cc.GLFW_KEY_MENU;
pub const last = cc.GLFW_KEY_LAST; pub const last = cc.GLFW_KEY_LAST;
// TODO(keyboard layout) /// Returns the layout-specific name of the specified printable key.
// /// Returns the layout-specific name of the specified printable key. ///
// /// /// This function returns the name of the specified printable key, encoded as UTF-8. This is
// /// This function returns the name of the specified printable key, encoded as /// typically the character that key would produce without any modifier keys, intended for
// /// UTF-8. This is typically the character that key would produce without any /// displaying key bindings to the user. For dead keys, it is typically the diacritic it would add
// /// modifier keys, intended for displaying key bindings to the user. For dead /// to a character.
// /// keys, it is typically the diacritic it would add to a character. ///
// /// /// __Do not use this function__ for text input (see input_char). You will break text input for many
// /// __Do not use this function__ for [text input](@ref input_char). You will /// languages even if it happens to work for yours.
// /// break text input for many languages even if it happens to work for yours. ///
// /// /// If the key is `glfw.key.unknown`, the scancode is used to identify the key, otherwise the
// /// If the key is `GLFW_KEY_UNKNOWN`, the scancode is used to identify the key, /// scancode is ignored. If you specify a non-printable key, or `glfw.key.unknown` and a scancode
// /// otherwise the scancode is ignored. If you specify a non-printable key, or /// that maps to a non-printable key, this function returns null but does not emit an error.
// /// `GLFW_KEY_UNKNOWN` and a scancode that maps to a non-printable key, this ///
// /// function returns null but does not emit an error. /// This behavior allows you to always pass in the arguments in the key callback (see input_key)
// /// /// without modification.
// /// This behavior allows you to always pass in the arguments in the ///
// /// [key callback](@ref input_key) without modification. /// The printable keys are:
// /// ///
// /// The printable keys are: /// - `glfw.key.apostrophe`
// /// - `GLFW_KEY_APOSTROPHE` /// - `glfw.key.comma`
// /// - `GLFW_KEY_COMMA` /// - `glfw.key.minus`
// /// - `GLFW_KEY_MINUS` /// - `glfw.key.period`
// /// - `GLFW_KEY_PERIOD` /// - `glfw.key.slash`
// /// - `GLFW_KEY_SLASH` /// - `glfw.key.semicolon`
// /// - `GLFW_KEY_SEMICOLON` /// - `glfw.key.equal`
// /// - `GLFW_KEY_EQUAL` /// - `glfw.key.left_bracket`
// /// - `GLFW_KEY_LEFT_BRACKET` /// - `glfw.key.right_bracket`
// /// - `GLFW_KEY_RIGHT_BRACKET` /// - `glfw.key.backslash`
// /// - `GLFW_KEY_BACKSLASH` /// - `glfw.key.world_1`
// /// - `GLFW_KEY_WORLD_1` /// - `glfw.key.world_2`
// /// - `GLFW_KEY_WORLD_2` /// - `glfw.key.0` to `glfw.key.9`
// /// - `GLFW_KEY_0` to `GLFW_KEY_9` /// - `glfw.key.a` to `glfw.key.z`
// /// - `GLFW_KEY_A` to `GLFW_KEY_Z` /// - `glfw.key.kp_0` to `glfw.key.kp_9`
// /// - `GLFW_KEY_KP_0` to `GLFW_KEY_KP_9` /// - `glfw.key.kp_decimal`
// /// - `GLFW_KEY_KP_DECIMAL` /// - `glfw.key.kp_divide`
// /// - `GLFW_KEY_KP_DIVIDE` /// - `glfw.key.kp_multiply`
// /// - `GLFW_KEY_KP_MULTIPLY` /// - `glfw.key.kp_subtract`
// /// - `GLFW_KEY_KP_SUBTRACT` /// - `glfw.key.kp_add`
// /// - `GLFW_KEY_KP_ADD` /// - `glfw.key.kp_equal`
// /// - `GLFW_KEY_KP_EQUAL` ///
// /// /// Names for printable keys depend on keyboard layout, while names for non-printable keys are the
// /// Names for printable keys depend on keyboard layout, while names for /// same across layouts but depend on the application language and should be localized along with
// /// non-printable keys are the same across layouts but depend on the application /// other user interface text.
// /// language and should be localized along with other user interface text. ///
// /// /// @param[in] key The key to query, or `glfw.key.unknown`.
// /// @param[in] key The key to query, or `GLFW_KEY_UNKNOWN`. /// @param[in] scancode The scancode of the key to query.
// /// @param[in] scancode The scancode of the key to query. /// @return The UTF-8 encoded, layout-specific name of the key, or null.
// /// @return The UTF-8 encoded, layout-specific name of the key, or null. ///
// /// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError. ///
// /// /// The contents of the returned string may change when a keyboard layout change event is received.
// /// The contents of the returned string may change when a keyboard ///
// /// layout change event is received. /// @pointer_lifetime The returned string is allocated and freed by GLFW. You should not free it
// /// /// yourself. It is valid until the library is terminated.
// /// @pointer_lifetime The returned string is allocated and freed by GLFW. You ///
// /// should not free it yourself. It is valid until the library is terminated. /// @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_name
// /// pub inline fn getName(key: isize, scancode: isize) Error![*c]const u8 {
// /// see also: input_key_name const name = cc.glfwGetKeyName(@intCast(c_int, key), @intCast(c_int, scancode));
// /// try getError();
// /// return name;
// /// @ingroup input }
// GLFWAPI const char* glfwGetKeyName(int key, int scancode);
// TODO(keyboard scancode) // TODO(keyboard scancode)
// /// Returns the platform-specific scancode of the specified key. // /// Returns the platform-specific scancode of the specified key.
// /// // ///
// /// This function returns the platform-specific scancode of the specified key. // /// This function returns the platform-specific scancode of the specified key.
// /// // ///
// /// If the key is `GLFW_KEY_UNKNOWN` or does not exist on the keyboard this // /// If the key is `glfw.key.UNKNOWN` or does not exist on the keyboard this
// /// method will return `-1`. // /// method will return `-1`.
// /// // ///
// /// @param[in] key Any [named key](@ref keys). // /// @param[in] key Any [named key](@ref keys).
@ -233,3 +235,11 @@ pub const last = cc.GLFW_KEY_LAST;
// /// // ///
// /// @ingroup input // /// @ingroup input
// GLFWAPI int glfwGetKeyScancode(int key); // GLFWAPI int glfwGetKeyScancode(int key);
test "getName" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
_ = glfw.key.getName(glfw.key.a, 0) catch |err| std.debug.print("failed to get key name, not supported? error={}\n", .{err});
}

View file

@ -297,6 +297,7 @@ test "getVersionString" {
_ = Monitor; _ = Monitor;
_ = GammaRamp; _ = GammaRamp;
_ = Image; _ = Image;
_ = key;
_ = Joystick; _ = Joystick;
_ = VideoMode; _ = VideoMode;
_ = Window; _ = Window;