1#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
6#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7#[repr(u8)]
8#[allow(missing_docs)]
9pub enum Key {
10 Grave,
12 Num1,
13 Num2,
14 Num3,
15 Num4,
16 Num5,
17 Num6,
18 Num7,
19 Num8,
20 Num9,
21 Num0,
22 Minus,
23 Equal,
24 Backspace,
25 Tab,
27 Q,
28 W,
29 E,
30 R,
31 T,
32 Y,
33 U,
34 I,
35 O,
36 P,
37 LeftBracket,
38 RightBracket,
39 Backslash,
40 Fn,
42 Shift,
43 A,
44 S,
45 D,
46 F,
47 G,
48 H,
49 J,
50 K,
51 L,
52 Semicolon,
53 Quote,
54 Enter,
55 Ctrl,
57 Opt,
58 Alt,
59 Z,
60 X,
61 C,
62 V,
63 B,
64 N,
65 M,
66 Comma,
67 Period,
68 Slash,
69 Space,
70}
71
72impl Key {
73 pub const COUNT: usize = Key::Space as usize + 1;
75
76 pub const fn legend(self) -> Option<(char, char)> {
79 use Key::*;
80 Some(match self {
81 Grave => ('`', '~'),
82 Num1 => ('1', '!'),
83 Num2 => ('2', '@'),
84 Num3 => ('3', '#'),
85 Num4 => ('4', '$'),
86 Num5 => ('5', '%'),
87 Num6 => ('6', '^'),
88 Num7 => ('7', '&'),
89 Num8 => ('8', '*'),
90 Num9 => ('9', '('),
91 Num0 => ('0', ')'),
92 Minus => ('-', '_'),
93 Equal => ('=', '+'),
94 Q => ('q', 'Q'),
95 W => ('w', 'W'),
96 E => ('e', 'E'),
97 R => ('r', 'R'),
98 T => ('t', 'T'),
99 Y => ('y', 'Y'),
100 U => ('u', 'U'),
101 I => ('i', 'I'),
102 O => ('o', 'O'),
103 P => ('p', 'P'),
104 LeftBracket => ('[', '{'),
105 RightBracket => (']', '}'),
106 Backslash => ('\\', '|'),
107 A => ('a', 'A'),
108 S => ('s', 'S'),
109 D => ('d', 'D'),
110 F => ('f', 'F'),
111 G => ('g', 'G'),
112 H => ('h', 'H'),
113 J => ('j', 'J'),
114 K => ('k', 'K'),
115 L => ('l', 'L'),
116 Semicolon => (';', ':'),
117 Quote => ('\'', '"'),
118 Z => ('z', 'Z'),
119 X => ('x', 'X'),
120 C => ('c', 'C'),
121 V => ('v', 'V'),
122 B => ('b', 'B'),
123 N => ('n', 'N'),
124 M => ('m', 'M'),
125 Comma => (',', '<'),
126 Period => ('.', '>'),
127 Slash => ('/', '?'),
128 Space => (' ', ' '),
129 Backspace | Tab | Fn | Shift | Enter | Ctrl | Opt | Alt => return None,
130 })
131 }
132
133 pub const fn modifier(self) -> Option<Modifiers> {
135 match self {
136 Key::Shift => Some(Modifiers::SHIFT),
137 Key::Ctrl => Some(Modifiers::CTRL),
138 Key::Alt => Some(Modifiers::ALT),
139 Key::Opt => Some(Modifiers::OPT),
140 Key::Fn => Some(Modifiers::FN),
141 _ => None,
142 }
143 }
144
145 pub(crate) const fn bit(self) -> u64 {
146 1 << self as u8
147 }
148}
149
150#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
152#[cfg_attr(feature = "defmt", derive(defmt::Format))]
153pub struct Modifiers(u8);
154
155impl Modifiers {
156 pub const NONE: Self = Self(0);
158 pub const SHIFT: Self = Self(1 << 0);
160 pub const CTRL: Self = Self(1 << 1);
162 pub const ALT: Self = Self(1 << 2);
164 pub const OPT: Self = Self(1 << 3);
166 pub const FN: Self = Self(1 << 4);
168
169 pub const fn contains(self, other: Self) -> bool {
171 self.0 & other.0 == other.0
172 }
173
174 pub const fn union(self, other: Self) -> Self {
176 Self(self.0 | other.0)
177 }
178
179 pub const fn is_empty(self) -> bool {
181 self.0 == 0
182 }
183}
184
185impl core::ops::BitOr for Modifiers {
186 type Output = Self;
187 fn bitor(self, rhs: Self) -> Self {
188 self.union(rhs)
189 }
190}
191
192#[derive(Clone, Copy, Debug, PartialEq, Eq)]
194#[cfg_attr(feature = "defmt", derive(defmt::Format))]
195pub enum Action {
196 Char(char),
198 Enter,
200 Backspace,
202 Delete,
204 Tab,
206 Escape,
208 Up,
210 Down,
212 Left,
214 Right,
216 F(u8),
218}