human/key.rs
1// Human
2// Copyright © 2020-2021 Jeron Aldaron Lau.
3//
4// Licensed under any of:
5// - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
6// - MIT License (https://mit-license.org/)
7// - Boost Software License, Version 1.0 (https://www.boost.org/LICENSE_1_0.txt)
8// At your choosing (See accompanying files LICENSE_APACHE_2_0.txt,
9// LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt).
10
11use std::fmt::{Debug, Formatter, Result};
12
13const MOD_SHIFT: u8 = 0b0000_0001;
14const MOD_CTRL: u8 = 0b0000_0010;
15const MOD_ALT: u8 = 0b0000_0100;
16
17/// Modifier state.
18#[repr(transparent)]
19#[derive(Copy, Clone, Default)]
20pub struct Mod(u8);
21
22impl Mod {
23 /// Check if no modifiers are held down.
24 #[inline(always)]
25 pub fn none(self) -> bool {
26 self.0 == 0
27 }
28
29 /// Check if SHIFT is held down.
30 #[inline(always)]
31 pub fn shift(self) -> bool {
32 (self.0 & MOD_SHIFT) != 0
33 }
34
35 /// Check if CTRL or CMD is held down.
36 #[inline(always)]
37 pub fn ctrl(self) -> bool {
38 (self.0 & MOD_CTRL) != 0
39 }
40
41 /// Check if ALT or OPTION is held down.
42 #[inline(always)]
43 pub fn alt(self) -> bool {
44 (self.0 & MOD_ALT) != 0
45 }
46
47 /// No modifiers.
48 #[inline(always)]
49 pub fn new() -> Self {
50 Mod(0)
51 }
52
53 /// Add shift key.
54 #[inline(always)]
55 pub fn add_shift(self) -> Self {
56 Self(self.0 | MOD_SHIFT)
57 }
58
59 /// Add control key.
60 #[inline(always)]
61 pub fn add_ctrl(self) -> Self {
62 Self(self.0 | MOD_CTRL)
63 }
64
65 /// Add alt key.
66 #[inline(always)]
67 pub fn add_alt(self) -> Self {
68 Self(self.0 | MOD_ALT)
69 }
70}
71
72impl Debug for Mod {
73 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
74 if self.ctrl() {
75 if self.alt() {
76 if self.shift() {
77 write!(f, "Ctrl + Alt + Shift")
78 } else {
79 write!(f, "Ctrl + Alt")
80 }
81 } else {
82 write!(f, "Ctrl")
83 }
84 } else if self.alt() {
85 if self.shift() {
86 write!(f, "Alt + Shift")
87 } else {
88 write!(f, "Alt")
89 }
90 } else if self.shift() {
91 write!(f, "Shift")
92 } else {
93 write!(f, "None")
94 }
95 }
96}
97
98/// Input keycode for a key on a keyboard.
99#[repr(u8)]
100#[derive(Debug, Copy, Clone)]
101#[non_exhaustive]
102pub enum Key {
103 /// Also known as the ESCAPE key.
104 Back = 0x00u8,
105 /// Numeric 1 on either top row or numpad.
106 One = 0x01,
107 /// Numeric 2 on either top row or numpad.
108 Two = 0x02,
109 /// Numeric 3 on either top row or numpad.
110 Three = 0x03,
111 /// Numeric 4 on either top row or numpad.
112 Four = 0x04,
113 /// Numeric 5 on either top row or numpad.
114 Five = 0x05,
115 /// Numeric 6 on either top row or numpad.
116 Six = 0x06,
117 /// Numeric 7 on either top row or numpad.
118 Seven = 0x07,
119 /// Numeric 8 on either top row or numpad.
120 Eight = 0x08,
121 /// Numeric 9 on either top row or numpad.
122 Nine = 0x09,
123 /// Numeric 0 on either top row or numpad.
124 Zero = 0x0A,
125 /// Minus / Underscore Key
126 Minus = 0x0B,
127 /// Equal Sign / Plus Key
128 Equal = 0x0C,
129 /// Backtick / Tilde Key
130 Backtick = 0x0D,
131 /// Tab
132 Tab = 0x0E,
133 /// Q (may be named by a different glyph depending on language of user).
134 Q = 0x0F,
135 /// W (may be named by a different glyph depending on language of user).
136 W = 0x10,
137 /// E (may be named by a different glyph depending on language of user).
138 E = 0x11,
139 /// R (may be named by a different glyph depending on language of user).
140 R = 0x12,
141 /// T (may be named by a different glyph depending on language of user).
142 T = 0x13,
143 /// Y (may be named by a different glyph depending on language of user).
144 Y = 0x14,
145 /// U (may be named by a different glyph depending on language of user).
146 U = 0x15,
147 /// I (may be named by a different glyph depending on language of user).
148 I = 0x16,
149 /// U (may be named by a different glyph depending on language of user).
150 O = 0x17,
151 /// I (may be named by a different glyph depending on language of user).
152 P = 0x18,
153 /// [ (may be named by a different glyph depending on language of user).
154 BracketOpen = 0x19,
155 /// ] (may be named by a different glyph depending on language of user).
156 BracketClose = 0x1A,
157 /// Backspace.
158 Backspace = 0x1B,
159 /// Env (Also known as: Win, Super, Cmd, Search) Key
160 Env = 0x1C,
161 /// A (may be named by a different glyph depending on language of user).
162 A = 0x1D,
163 /// S (may be named by a different glyph depending on language of user).
164 S = 0x1E,
165 /// D (may be named by a different glyph depending on language of user).
166 D = 0x1F,
167 /// F (may be named by a different glyph depending on language of user).
168 F = 0x20,
169 /// G (may be named by a different glyph depending on language of user).
170 G = 0x21,
171 /// H (may be named by a different glyph depending on language of user).
172 H = 0x22,
173 /// J (may be named by a different glyph depending on language of user).
174 J = 0x23,
175 /// K (may be named by a different glyph depending on language of user).
176 K = 0x24,
177 /// L (may be named by a different glyph depending on language of user).
178 L = 0x25,
179 /// ; (may be named by a different glyph depending on language of user).
180 Semicolon = 0x26,
181 /// ' (may be named by a different glyph depending on language of user).
182 Apostrophe = 0x27,
183 /// Enter (Also Return).
184 Enter = 0x28,
185 /// Left Shift Key
186 LShift = 0x29,
187 /// Z (may be named by a different glyph depending on language of user).
188 Z = 0x2A,
189 /// X (may be named by a different glyph depending on language of user).
190 X = 0x2B,
191 /// C (may be named by a different glyph depending on language of user).
192 C = 0x2C,
193 /// V (may be named by a different glyph depending on language of user).
194 V = 0x2D,
195 /// B (may be named by a different glyph depending on language of user).
196 B = 0x2E,
197 /// N (may be named by a different glyph depending on language of user).
198 N = 0x2F,
199 /// M (may be named by a different glyph depending on language of user).
200 M = 0x30,
201 /// , (may be named by a different glyph depending on language of user).
202 Comma = 0x31,
203 /// . (may be named by a different glyph depending on language of user).
204 Period = 0x32,
205 /// / (may be named by a different glyph depending on language of user).
206 Slash = 0x33,
207 /// \ (may be named by a different glyph depending on language of user).
208 Backslash = 0x34,
209 /// Up Arrow
210 Up = 0x35,
211 /// Right Shift Key
212 RShift = 0x36,
213 /// Left control
214 LCtrl = 0x37,
215 /// Left alt
216 LAlt = 0x38,
217 /// Space (or Left Thumb Button)
218 Space = 0x39,
219 /// Compose Key (Alt Gr, Right Thumb Button, NumLock, ScrLk Key)
220 Compose = 0x3A,
221 /// Right Alt
222 RAlt = 0x3B,
223 /// Right Control
224 RCtrl = 0x3C,
225 /// Left Arrow Key
226 Left = 0x3D,
227 /// Down Arrow Key
228 Down = 0x3E,
229 /// Right Arrow Key
230 Right = 0x3F,
231
232 // Back = 0x40u8,
233 /// F1 Key
234 F1 = 0x41,
235 /// F2 Key
236 F2 = 0x42,
237 /// F3 Key
238 F3 = 0x43,
239 /// F4 Key
240 F4 = 0x44,
241 /// F5 Key
242 F5 = 0x45,
243 /// F6 Key
244 F6 = 0x46,
245 /// F7 Key
246 F7 = 0x47,
247 /// F8 Key
248 F8 = 0x48,
249 /// F9 Key
250 F9 = 0x49,
251 /// F10 Key
252 F10 = 0x4A,
253 /// F11 Key
254 F11 = 0x4B,
255 /// F12 Key
256 F12 = 0x4C,
257 /// Insert Key
258 Insert = 0x4D,
259 // Tab = 0x4E,
260 // Q = 0x4F,
261 // W = 0x50,
262 // E = 0x51,
263 // R = 0x52,
264 // T = 0x53,
265 // Y = 0x54,
266 // U = 0x55,
267 // I = 0x56,
268 // O = 0x57,
269 // P = 0x58,
270 // BracketOpen = 0x19,
271 // BracketClose = 0x1A,
272 /// The delete key
273 Delete = 0x5B,
274 /// The Caps Lock Key
275 CapsLock = 0x5C,
276 // A = 0x1D,
277 // S = 0x1E,
278 // D = 0x1F,
279 // F = 0x20,
280 // G = 0x21,
281 // H = 0x22,
282 // J = 0x23,
283 // K = 0x24,
284 // L = 0x25,
285 // Semicolon = 0x26,
286 // Apostrophe = 0x27,
287 // NumpadEnter = 0x28,
288 // LShift = 0x29,
289 // Z = 0x2A,
290 // X = 0x2B,
291 // C = 0x2C,
292 // V = 0x2D,
293 // B = 0x2E,
294 // N = 0x2F,
295 // M = 0x30,
296 // Comma = 0x31,
297 // Period = 0x32,
298 // Slash = 0x33,
299 // Backslash = 0x34,
300 /// Page Up
301 PageUp = 0x75,
302 // RShift = 0x36,
303 // LCtrl = 0x37,
304 // LAlt = 0x38,
305 /// Pause Key
306 Pause = 0x79,
307 // Compose = 0x3A,
308 // RAlt = 0x3B,
309 /// Context Menu
310 Menu = 0x7C,
311 /// Home
312 Home = 0x7D,
313 /// Page Down
314 PageDown = 0x7E,
315 /// End
316 End = 0x7F,
317}