summaryrefslogtreecommitdiff
path: root/src/keys.zig
blob: 7e13ba0fe35a3e3a98493c1c102c00571e3bd637 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! USB HID keyboard usages: SDL scancodes use them, and browsers map
//! `KeyboardEvent.code` onto them, so recordings mean the same key everywhere.
pub const Key = enum(u8) {
    a = 4,
    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,
    @"1",
    @"2",
    @"3",
    @"4",
    @"5",
    @"6",
    @"7",
    @"8",
    @"9",
    @"0",
    @"return",
    escape,
    backspace,
    tab,
    space,
    minus,
    equals,
    left_bracket,
    right_bracket,
    backslash,
    semicolon = 51,
    apostrophe,
    grave,
    comma,
    period,
    slash,
    f1 = 58,
    f2,
    f3,
    f4,
    f5,
    f6,
    f7,
    f8,
    f9,
    f10,
    f11,
    f12,
    home = 74,
    page_up,
    delete,
    end,
    page_down,
    right,
    left,
    down,
    up,
    keypad_enter = 88,
    return2 = 158,
    left_ctrl = 224,
    left_shift,
    left_alt,
    left_gui,
    right_ctrl,
    right_shift,
    right_alt,
    right_gui,
    _,

    pub const Mod = struct {
        pub const shift: u32 = 1;
        pub const ctrl: u32 = 2;
        pub const alt: u32 = 4;
        pub const gui: u32 = 8;
    };

    pub fn isEnter(key: Key) bool {
        return key == .@"return" or key == .keypad_enter or key == .return2;
    }
};

test "HID values match the specification's anchors" {
    const std = @import("std");
    try std.testing.expectEqual(4, @backingInt(Key.a));
    try std.testing.expectEqual(39, @backingInt(Key.@"0"));
    try std.testing.expectEqual(53, @backingInt(Key.grave));
    try std.testing.expectEqual(82, @backingInt(Key.up));
}