From e787a79702dcedc84deb5b80ede0d84c934be39b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Aug 2023 09:16:29 -0700 Subject: [PATCH] input: printable, comment on what magic indexes mean --- src/input/key.zig | 73 ++++++++++++++++++++++++++++++++++++++++++ src/input/keycodes.zig | 14 ++++---- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/input/key.zig b/src/input/key.zig index 52ccc5ffd..6ef6ae38d 100644 --- a/src/input/key.zig +++ b/src/input/key.zig @@ -243,4 +243,77 @@ pub const Key = enum(c_int) { else => null, }; } + + /// True if this key represents a printable character. + pub fn printable(self: Key) bool { + return switch (self) { + .a, + .b, + .c, + .d, + .e, + .f, + .g, + .h, + .i, + .j, + .k, + .l, + .m, + .n, + .o, + .p, + .q, + .r, + .s, + .t, + .u, + .v, + .w, + .x, + .y, + .z, + .zero, + .one, + .two, + .three, + .four, + .five, + .six, + .seven, + .eight, + .nine, + .semicolon, + .space, + .apostrophe, + .comma, + .grave_accent, + .period, + .slash, + .minus, + .equal, + .left_bracket, + .right_bracket, + .backslash, + .kp_0, + .kp_1, + .kp_2, + .kp_3, + .kp_4, + .kp_5, + .kp_6, + .kp_7, + .kp_8, + .kp_9, + .kp_decimal, + .kp_divide, + .kp_multiply, + .kp_subtract, + .kp_add, + .kp_equal, + => true, + + else => false, + }; + } }; diff --git a/src/input/keycodes.zig b/src/input/keycodes.zig index 21fa0e1b5..d29158bc9 100644 --- a/src/input/keycodes.zig +++ b/src/input/keycodes.zig @@ -8,14 +8,12 @@ const Key = @import("key.zig").Key; /// The full list of entries for the current platform. pub const entries: []const Entry = entries: { - const native_idx = if (builtin.target.isDarwin()) - 4 - else if (builtin.target.isWindows()) - 3 - else if (builtin.target.isLinux()) - 2 - else - @compileError("unsupported platform"); + const native_idx = switch (builtin.os.tag) { + .macos => 4, // mac + .windows => 3, // win + .linux => 2, // xkb + else => @compileError("unsupported platform"), + }; var result: [raw_entries.len]Entry = undefined; for (raw_entries, 0..) |raw, i| {