1use postcard::experimental::max_size::MaxSize;
4use serde::{Deserialize, Serialize};
5use strum::FromRepr;
6
7use super::consumer::ConsumerKey;
8use super::system_control::SystemControlKey;
9use crate::modifier::ModifierCombination;
10
11#[repr(u8)]
13#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord, FromRepr, MaxSize)]
14#[cfg_attr(feature = "_codegen", derive(strum::EnumIter, strum::VariantNames))]
15#[cfg_attr(feature = "defmt", derive(defmt::Format))]
16#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
17#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
18pub enum HidKeyCode {
19 No = 0x0000,
21 ErrorRollover = 0x0001,
24 PostFail = 0x0002,
26 ErrorUndefined = 0x0003,
28 A = 0x0004,
30 B = 0x0005,
32 C = 0x0006,
34 D = 0x0007,
36 E = 0x0008,
38 F = 0x0009,
40 G = 0x000A,
42 H = 0x000B,
44 I = 0x000C,
46 J = 0x000D,
48 K = 0x000E,
50 L = 0x000F,
52 M = 0x0010,
54 N = 0x0011,
56 O = 0x0012,
58 P = 0x0013,
60 Q = 0x0014,
62 R = 0x0015,
64 S = 0x0016,
66 T = 0x0017,
68 U = 0x0018,
70 V = 0x0019,
72 W = 0x001A,
74 X = 0x001B,
76 Y = 0x001C,
78 Z = 0x001D,
80 Kc1 = 0x001E,
82 Kc2 = 0x001F,
84 Kc3 = 0x0020,
86 Kc4 = 0x0021,
88 Kc5 = 0x0022,
90 Kc6 = 0x0023,
92 Kc7 = 0x0024,
94 Kc8 = 0x0025,
96 Kc9 = 0x0026,
98 Kc0 = 0x0027,
100 Enter = 0x0028,
102 Escape = 0x0029,
104 Backspace = 0x002A,
106 Tab = 0x002B,
108 Space = 0x002C,
110 Minus = 0x002D,
112 Equal = 0x002E,
114 LeftBracket = 0x002F,
116 RightBracket = 0x0030,
118 Backslash = 0x0031,
120 NonusHash = 0x0032,
122 Semicolon = 0x0033,
124 Quote = 0x0034,
126 Grave = 0x0035,
128 Comma = 0x0036,
130 Dot = 0x0037,
132 Slash = 0x0038,
134 CapsLock = 0x0039,
136 F1 = 0x003A,
138 F2 = 0x003B,
140 F3 = 0x003C,
142 F4 = 0x003D,
144 F5 = 0x003E,
146 F6 = 0x003F,
148 F7 = 0x0040,
150 F8 = 0x0041,
152 F9 = 0x0042,
154 F10 = 0x0043,
156 F11 = 0x0044,
158 F12 = 0x0045,
160 PrintScreen = 0x0046,
162 ScrollLock = 0x0047,
164 Pause = 0x0048,
166 Insert = 0x0049,
168 Home = 0x004A,
170 PageUp = 0x004B,
172 Delete = 0x004C,
174 End = 0x004D,
176 PageDown = 0x004E,
178 Right = 0x004F,
180 Left = 0x0050,
182 Down = 0x0051,
184 Up = 0x0052,
186 NumLock = 0x0053,
188 KpSlash = 0x0054,
190 KpAsterisk = 0x0055,
192 KpMinus = 0x0056,
194 KpPlus = 0x0057,
196 KpEnter = 0x0058,
198 Kp1 = 0x0059,
200 Kp2 = 0x005A,
202 Kp3 = 0x005B,
204 Kp4 = 0x005C,
206 Kp5 = 0x005D,
208 Kp6 = 0x005E,
210 Kp7 = 0x005F,
212 Kp8 = 0x0060,
214 Kp9 = 0x0061,
216 Kp0 = 0x0062,
218 KpDot = 0x0063,
220 NonusBackslash = 0x0064,
222 Application = 0x0065,
224 KbPower = 0x0066,
226 KpEqual = 0x0067,
228 F13 = 0x0068,
230 F14 = 0x0069,
232 F15 = 0x006A,
234 F16 = 0x006B,
236 F17 = 0x006C,
238 F18 = 0x006D,
240 F19 = 0x006E,
242 F20 = 0x006F,
244 F21 = 0x0070,
246 F22 = 0x0071,
248 F23 = 0x0072,
250 F24 = 0x0073,
252 Execute = 0x0074,
253 Help = 0x0075,
254 Menu = 0x0076,
255 Select = 0x0077,
256 Stop = 0x0078,
257 Again = 0x0079,
258 Undo = 0x007A,
259 Cut = 0x007B,
260 Copy = 0x007C,
261 Paste = 0x007D,
262 Find = 0x007E,
263 KbMute = 0x007F,
265 KbVolumeUp = 0x0080,
267 KbVolumeDown = 0x0081,
269 LockingCapsLock = 0x0082,
271 LockingNumLock = 0x0083,
273 LockingScrollLock = 0x0084,
275 KpComma = 0x0085,
276 KpEqualAs400 = 0x0086,
277 International1 = 0x0087,
278 International2 = 0x0088,
279 International3 = 0x0089,
280 International4 = 0x008A,
281 International5 = 0x008B,
282 International6 = 0x008C,
283 International7 = 0x008D,
284 International8 = 0x008E,
285 International9 = 0x008F,
286 Language1 = 0x0090,
287 Language2 = 0x0091,
288 Language3 = 0x0092,
289 Language4 = 0x0093,
290 Language5 = 0x0094,
291 Language6 = 0x0095,
292 Language7 = 0x0096,
293 Language8 = 0x0097,
294 Language9 = 0x0098,
295 AlternateErase = 0x0099,
296 SystemRequest = 0x009A,
297 Cancel = 0x009B,
298 Clear = 0x009C,
299 Prior = 0x009D,
300 Return = 0x009E,
301 Separator = 0x009F,
302 Out = 0x00A0,
303 Oper = 0x00A1,
304 ClearAgain = 0x00A2,
305 Crsel = 0x00A3,
306 Exsel = 0x00A4,
307 SystemPower = 0x00A5,
308 SystemSleep = 0x00A6,
309 SystemWake = 0x00A7,
310 AudioMute = 0x00A8,
311 AudioVolUp = 0x00A9,
312 AudioVolDown = 0x00AA,
313 MediaNextTrack = 0x00AB,
314 MediaPrevTrack = 0x00AC,
315 MediaStop = 0x00AD,
316 MediaPlayPause = 0x00AE,
317 MediaSelect = 0x00AF,
318 MediaEject = 0x00B0,
319 Mail = 0x00B1,
320 Calculator = 0x00B2,
321 MyComputer = 0x00B3,
322 WwwSearch = 0x00B4,
323 WwwHome = 0x00B5,
324 WwwBack = 0x00B6,
325 WwwForward = 0x00B7,
326 WwwStop = 0x00B8,
327 WwwRefresh = 0x00B9,
328 WwwFavorites = 0x00BA,
329 MediaFastForward = 0x00BB,
330 MediaRewind = 0x00BC,
331 BrightnessUp = 0x00BD,
333 BrightnessDown = 0x00BE,
335 ControlPanel = 0x00BF,
336 Assistant = 0x00C0,
337 MissionControl = 0x00C1,
338 Launchpad = 0x00C2,
339 MouseUp = 0x00CD,
341 MouseDown = 0x00CE,
343 MouseLeft = 0x00CF,
345 MouseRight = 0x00D0,
347 MouseBtn1 = 0x00D1,
349 MouseBtn2 = 0x00D2,
351 MouseBtn3 = 0x00D3,
353 MouseBtn4 = 0x00D4,
355 MouseBtn5 = 0x00D5,
357 MouseBtn6 = 0x00D6,
358 MouseBtn7 = 0x00D7,
359 MouseBtn8 = 0x00D8,
360 MouseWheelUp = 0x00D9,
361 MouseWheelDown = 0x00DA,
362 MouseWheelLeft = 0x00DB,
363 MouseWheelRight = 0x00DC,
364 MouseAccel0 = 0x00DD,
365 MouseAccel1 = 0x00DE,
366 MouseAccel2 = 0x00DF,
367 LCtrl = 0x00E0,
369 LShift = 0x00E1,
371 LAlt = 0x00E2,
373 LGui = 0x00E3,
375 RCtrl = 0x00E4,
377 RShift = 0x00E5,
379 RAlt = 0x00E6,
381 RGui = 0x00E7,
383}
384
385impl HidKeyCode {
386 #[cfg(feature = "_codegen")]
388 pub fn all() -> impl Iterator<Item = Self> {
389 <Self as strum::IntoEnumIterator>::iter()
390 }
391
392 pub fn is_simple_key(self) -> bool {
394 HidKeyCode::No <= self && self <= HidKeyCode::MouseAccel2
395 }
396
397 pub fn is_modifier(self) -> bool {
399 HidKeyCode::LCtrl <= self && self <= HidKeyCode::RGui
400 }
401
402 pub fn is_mouse_key(self) -> bool {
404 HidKeyCode::MouseUp <= self && self <= HidKeyCode::MouseAccel2
405 }
406
407 pub fn to_hid_modifiers(self) -> ModifierCombination {
410 match self {
411 HidKeyCode::LCtrl => ModifierCombination::LCTRL,
412 HidKeyCode::LShift => ModifierCombination::LSHIFT,
413 HidKeyCode::LAlt => ModifierCombination::LALT,
414 HidKeyCode::LGui => ModifierCombination::LGUI,
415 HidKeyCode::RCtrl => ModifierCombination::RCTRL,
416 HidKeyCode::RShift => ModifierCombination::RSHIFT,
417 HidKeyCode::RAlt => ModifierCombination::RALT,
418 HidKeyCode::RGui => ModifierCombination::RGUI,
419 _ => ModifierCombination::new(),
420 }
421 }
422
423 pub fn is_caps_word_continue_key(self) -> bool {
425 if self >= HidKeyCode::A && self <= HidKeyCode::Z {
426 return true;
427 }
428 if self >= HidKeyCode::Kc1 && self <= HidKeyCode::Kc0 {
429 return true;
430 }
431 if self == HidKeyCode::Minus || self == HidKeyCode::Backspace || self == HidKeyCode::Delete {
432 return true;
433 }
434 false
435 }
436
437 pub fn is_caps_word_shifted_key(self) -> bool {
439 if self >= HidKeyCode::A && self <= HidKeyCode::Z {
440 return true;
441 }
442 if self == HidKeyCode::Minus {
443 return true;
444 }
445 false
446 }
447
448 pub fn process_as_consumer(&self) -> Option<ConsumerKey> {
450 match self {
451 HidKeyCode::AudioMute => Some(ConsumerKey::Mute),
452 HidKeyCode::AudioVolUp => Some(ConsumerKey::VolumeIncrement),
453 HidKeyCode::AudioVolDown => Some(ConsumerKey::VolumeDecrement),
454 HidKeyCode::MediaNextTrack => Some(ConsumerKey::NextTrack),
455 HidKeyCode::MediaPrevTrack => Some(ConsumerKey::PrevTrack),
456 HidKeyCode::MediaStop => Some(ConsumerKey::StopPlay),
457 HidKeyCode::MediaPlayPause => Some(ConsumerKey::PlayPause),
458 HidKeyCode::MediaSelect => Some(ConsumerKey::Record),
459 HidKeyCode::MediaEject => Some(ConsumerKey::Eject),
460 HidKeyCode::Mail => Some(ConsumerKey::Email),
461 HidKeyCode::Calculator => Some(ConsumerKey::Calculator),
462 HidKeyCode::MyComputer => Some(ConsumerKey::LocalBrowser),
463 HidKeyCode::WwwSearch => Some(ConsumerKey::Search),
464 HidKeyCode::WwwHome => Some(ConsumerKey::Home),
465 HidKeyCode::WwwBack => Some(ConsumerKey::Back),
466 HidKeyCode::WwwForward => Some(ConsumerKey::Forward),
467 HidKeyCode::WwwStop => Some(ConsumerKey::Stop),
468 HidKeyCode::WwwRefresh => Some(ConsumerKey::Refresh),
469 HidKeyCode::WwwFavorites => Some(ConsumerKey::Bookmarks),
470 HidKeyCode::MediaFastForward => Some(ConsumerKey::FastForward),
471 HidKeyCode::MediaRewind => Some(ConsumerKey::Rewind),
472 HidKeyCode::BrightnessUp => Some(ConsumerKey::BrightnessUp),
473 HidKeyCode::BrightnessDown => Some(ConsumerKey::BrightnessDown),
474 HidKeyCode::ControlPanel => Some(ConsumerKey::ControlPanel),
475 HidKeyCode::Assistant => Some(ConsumerKey::Assistant),
476 HidKeyCode::MissionControl => Some(ConsumerKey::DesktopShowAllWindows),
477 HidKeyCode::Launchpad => Some(ConsumerKey::AcSoftKeyLeft),
478 _ => None,
479 }
480 }
481
482 pub fn process_as_system_control(&self) -> Option<SystemControlKey> {
484 match self {
485 HidKeyCode::SystemPower => Some(SystemControlKey::PowerDown),
486 HidKeyCode::SystemSleep => Some(SystemControlKey::Sleep),
487 HidKeyCode::SystemWake => Some(SystemControlKey::WakeUp),
488 _ => None,
489 }
490 }
491}
492
493impl From<u8> for HidKeyCode {
494 fn from(value: u8) -> Self {
495 Self::from_repr(value).unwrap_or(HidKeyCode::No)
496 }
497}