1#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
7pub enum Key {
8 ArrowDown,
11 ArrowLeft,
12 ArrowRight,
13 ArrowUp,
14
15 Escape,
16 Tab,
17 Backspace,
18 Enter,
19 Space,
20
21 Insert,
22 Delete,
23 Home,
24 End,
25 PageUp,
26 PageDown,
27
28 Copy,
29 Cut,
30 Paste,
31
32 Colon,
36
37 Comma,
39
40 Backslash,
42
43 Slash,
45
46 Pipe,
48
49 Questionmark,
51
52 Exclamationmark,
54
55 OpenBracket,
57
58 CloseBracket,
60
61 OpenCurlyBracket,
63
64 CloseCurlyBracket,
66
67 Backtick,
69
70 Minus,
72
73 Period,
75
76 Plus,
78
79 Equals,
81
82 Semicolon,
84
85 Quote,
87
88 Num0,
92
93 Num1,
95
96 Num2,
98
99 Num3,
101
102 Num4,
104
105 Num5,
107
108 Num6,
110
111 Num7,
113
114 Num8,
116
117 Num9,
119
120 A, B,
124 C, D, E, F, G, H, I, J, K, L,
134 M,
135 N,
136 O, P, Q,
139 R, S, T, U, V, W, X, Y,
147 Z, F1,
152 F2,
153 F3,
154 F4,
155 F5, F6,
157 F7,
158 F8,
159 F9,
160 F10,
161 F11,
162 F12,
163 F13,
164 F14,
165 F15,
166 F16,
167 F17,
168 F18,
169 F19,
170 F20,
171 F21,
172 F22,
173 F23,
174 F24,
175 F25,
176 F26,
177 F27,
178 F28,
179 F29,
180 F30,
181 F31,
182 F32,
183 F33,
184 F34,
185 F35,
186
187 BrowserBack,
191
192 ShiftLeft,
200
201 ShiftRight,
203
204 ControlLeft,
206
207 ControlRight,
209
210 AltLeft,
212
213 AltRight,
215
216 SuperLeft,
218
219 SuperRight,
221
222 IntlBackslash,
229 }
237
238impl Key {
239 pub const ALL: &'static [Self] = &[
241 Self::ArrowDown,
243 Self::ArrowLeft,
244 Self::ArrowRight,
245 Self::ArrowUp,
246 Self::Escape,
247 Self::Tab,
248 Self::Backspace,
249 Self::Enter,
250 Self::Insert,
251 Self::Delete,
252 Self::Home,
253 Self::End,
254 Self::PageUp,
255 Self::PageDown,
256 Self::Copy,
257 Self::Cut,
258 Self::Paste,
259 Self::Space,
261 Self::Colon,
262 Self::Comma,
263 Self::Minus,
264 Self::Period,
265 Self::Plus,
266 Self::Equals,
267 Self::Semicolon,
268 Self::OpenBracket,
269 Self::CloseBracket,
270 Self::OpenCurlyBracket,
271 Self::CloseCurlyBracket,
272 Self::Backtick,
273 Self::Backslash,
274 Self::Slash,
275 Self::Pipe,
276 Self::Questionmark,
277 Self::Exclamationmark,
278 Self::Quote,
279 Self::Num0,
281 Self::Num1,
282 Self::Num2,
283 Self::Num3,
284 Self::Num4,
285 Self::Num5,
286 Self::Num6,
287 Self::Num7,
288 Self::Num8,
289 Self::Num9,
290 Self::A,
292 Self::B,
293 Self::C,
294 Self::D,
295 Self::E,
296 Self::F,
297 Self::G,
298 Self::H,
299 Self::I,
300 Self::J,
301 Self::K,
302 Self::L,
303 Self::M,
304 Self::N,
305 Self::O,
306 Self::P,
307 Self::Q,
308 Self::R,
309 Self::S,
310 Self::T,
311 Self::U,
312 Self::V,
313 Self::W,
314 Self::X,
315 Self::Y,
316 Self::Z,
317 Self::F1,
319 Self::F2,
320 Self::F3,
321 Self::F4,
322 Self::F5,
323 Self::F6,
324 Self::F7,
325 Self::F8,
326 Self::F9,
327 Self::F10,
328 Self::F11,
329 Self::F12,
330 Self::F13,
331 Self::F14,
332 Self::F15,
333 Self::F16,
334 Self::F17,
335 Self::F18,
336 Self::F19,
337 Self::F20,
338 Self::F21,
339 Self::F22,
340 Self::F23,
341 Self::F24,
342 Self::F25,
343 Self::F26,
344 Self::F27,
345 Self::F28,
346 Self::F29,
347 Self::F30,
348 Self::F31,
349 Self::F32,
350 Self::F33,
351 Self::F34,
352 Self::F35,
353 Self::BrowserBack,
355 Self::ShiftLeft,
357 Self::ShiftRight,
358 Self::ControlLeft,
359 Self::ControlRight,
360 Self::AltLeft,
361 Self::AltRight,
362 Self::SuperLeft,
363 Self::SuperRight,
364 Self::IntlBackslash,
366 ];
367
368 pub fn from_name(key: &str) -> Option<Self> {
378 Some(match key {
379 "⏷" | "ArrowDown" | "Down" => Self::ArrowDown,
380 "⏴" | "ArrowLeft" | "Left" => Self::ArrowLeft,
381 "⏵" | "ArrowRight" | "Right" => Self::ArrowRight,
382 "⏶" | "ArrowUp" | "Up" => Self::ArrowUp,
383
384 "Escape" | "Esc" => Self::Escape,
385 "Tab" => Self::Tab,
386 "Backspace" => Self::Backspace,
387 "Enter" | "Return" | "NumpadEnter" => Self::Enter,
388
389 "Help" | "Insert" => Self::Insert,
390 "Delete" => Self::Delete,
391 "Home" => Self::Home,
392 "End" => Self::End,
393 "PageUp" => Self::PageUp,
394 "PageDown" => Self::PageDown,
395
396 "Copy" => Self::Copy,
397 "Cut" => Self::Cut,
398 "Paste" => Self::Paste,
399
400 " " | "Space" => Self::Space,
401 ":" | "Colon" => Self::Colon,
402 "," | "Comma" | "NumpadComma" => Self::Comma,
403 "-" | "−" | "Minus" | "NumpadSubtract" => Self::Minus,
404 "." | "Period" | "NumpadDecimal" => Self::Period,
405 "+" | "Plus" | "NumpadAdd" => Self::Plus,
406 "=" | "Equal" | "Equals" | "NumpadEqual" => Self::Equals,
407 ";" | "Semicolon" => Self::Semicolon,
408 "\\" | "Backslash" => Self::Backslash,
409 "/" | "Slash" | "NumpadDivide" => Self::Slash,
410 "|" | "Pipe" => Self::Pipe,
411 "?" | "Questionmark" => Self::Questionmark,
412 "!" | "Exclamationmark" => Self::Exclamationmark,
413 "[" | "OpenBracket" | "BracketLeft" => Self::OpenBracket,
414 "]" | "CloseBracket" | "BracketRight" => Self::CloseBracket,
415 "{" | "OpenCurlyBracket" => Self::OpenCurlyBracket,
416 "}" | "CloseCurlyBracket" => Self::CloseCurlyBracket,
417 "`" | "Backtick" | "Backquote" | "Grave" => Self::Backtick,
418 "'" | "Quote" => Self::Quote,
419
420 "0" | "Digit0" | "Numpad0" => Self::Num0,
421 "1" | "Digit1" | "Numpad1" => Self::Num1,
422 "2" | "Digit2" | "Numpad2" => Self::Num2,
423 "3" | "Digit3" | "Numpad3" => Self::Num3,
424 "4" | "Digit4" | "Numpad4" => Self::Num4,
425 "5" | "Digit5" | "Numpad5" => Self::Num5,
426 "6" | "Digit6" | "Numpad6" => Self::Num6,
427 "7" | "Digit7" | "Numpad7" => Self::Num7,
428 "8" | "Digit8" | "Numpad8" => Self::Num8,
429 "9" | "Digit9" | "Numpad9" => Self::Num9,
430
431 "a" | "A" | "KeyA" => Self::A,
432 "b" | "B" | "KeyB" => Self::B,
433 "c" | "C" | "KeyC" => Self::C,
434 "d" | "D" | "KeyD" => Self::D,
435 "e" | "E" | "KeyE" => Self::E,
436 "f" | "F" | "KeyF" => Self::F,
437 "g" | "G" | "KeyG" => Self::G,
438 "h" | "H" | "KeyH" => Self::H,
439 "i" | "I" | "KeyI" => Self::I,
440 "j" | "J" | "KeyJ" => Self::J,
441 "k" | "K" | "KeyK" => Self::K,
442 "l" | "L" | "KeyL" => Self::L,
443 "m" | "M" | "KeyM" => Self::M,
444 "n" | "N" | "KeyN" => Self::N,
445 "o" | "O" | "KeyO" => Self::O,
446 "p" | "P" | "KeyP" => Self::P,
447 "q" | "Q" | "KeyQ" => Self::Q,
448 "r" | "R" | "KeyR" => Self::R,
449 "s" | "S" | "KeyS" => Self::S,
450 "t" | "T" | "KeyT" => Self::T,
451 "u" | "U" | "KeyU" => Self::U,
452 "v" | "V" | "KeyV" => Self::V,
453 "w" | "W" | "KeyW" => Self::W,
454 "x" | "X" | "KeyX" => Self::X,
455 "y" | "Y" | "KeyY" => Self::Y,
456 "z" | "Z" | "KeyZ" => Self::Z,
457
458 "F1" => Self::F1,
459 "F2" => Self::F2,
460 "F3" => Self::F3,
461 "F4" => Self::F4,
462 "F5" => Self::F5,
463 "F6" => Self::F6,
464 "F7" => Self::F7,
465 "F8" => Self::F8,
466 "F9" => Self::F9,
467 "F10" => Self::F10,
468 "F11" => Self::F11,
469 "F12" => Self::F12,
470 "F13" => Self::F13,
471 "F14" => Self::F14,
472 "F15" => Self::F15,
473 "F16" => Self::F16,
474 "F17" => Self::F17,
475 "F18" => Self::F18,
476 "F19" => Self::F19,
477 "F20" => Self::F20,
478 "F21" => Self::F21,
479 "F22" => Self::F22,
480 "F23" => Self::F23,
481 "F24" => Self::F24,
482 "F25" => Self::F25,
483 "F26" => Self::F26,
484 "F27" => Self::F27,
485 "F28" => Self::F28,
486 "F29" => Self::F29,
487 "F30" => Self::F30,
488 "F31" => Self::F31,
489 "F32" => Self::F32,
490 "F33" => Self::F33,
491 "F34" => Self::F34,
492 "F35" => Self::F35,
493
494 "BrowserBack" => Self::BrowserBack,
495
496 "ShiftLeft" => Self::ShiftLeft,
497 "ShiftRight" => Self::ShiftRight,
498 "ControlLeft" => Self::ControlLeft,
499 "ControlRight" => Self::ControlRight,
500 "AltLeft" => Self::AltLeft,
501 "AltRight" => Self::AltRight,
502
503 "SuperLeft" | "MetaLeft" | "OSLeft" => Self::SuperLeft,
504 "SuperRight" | "MetaRight" | "OSRight" => Self::SuperRight,
505
506 "IntlBackslash" => Self::IntlBackslash,
507
508 _ => return None,
509 })
510 }
511
512 pub fn symbol_or_name(self) -> &'static str {
514 match self {
518 Self::ArrowDown => "⏷",
519 Self::ArrowLeft => "⏴",
520 Self::ArrowRight => "⏵",
521 Self::ArrowUp => "⏶",
522
523 Self::Colon => ":",
524 Self::Comma => ",",
525 Self::Minus => crate::MINUS_CHAR_STR,
526 Self::Period => ".",
527 Self::Plus => "+",
528 Self::Equals => "=",
529 Self::Semicolon => ";",
530 Self::Backslash => "\\",
531 Self::Slash => "/",
532 Self::Pipe => "|",
533 Self::Questionmark => "?",
534 Self::Exclamationmark => "!",
535 Self::OpenBracket => "[",
536 Self::CloseBracket => "]",
537 Self::OpenCurlyBracket => "{",
538 Self::CloseCurlyBracket => "}",
539 Self::Backtick => "`",
540
541 _ => self.name(),
542 }
543 }
544
545 pub fn name(self) -> &'static str {
547 match self {
548 Self::ArrowDown => "Down",
549 Self::ArrowLeft => "Left",
550 Self::ArrowRight => "Right",
551 Self::ArrowUp => "Up",
552
553 Self::Escape => "Escape",
554 Self::Tab => "Tab",
555 Self::Backspace => "Backspace",
556 Self::Enter => "Enter",
557
558 Self::Insert => "Insert",
559 Self::Delete => "Delete",
560 Self::Home => "Home",
561 Self::End => "End",
562 Self::PageUp => "PageUp",
563 Self::PageDown => "PageDown",
564
565 Self::Copy => "Copy",
566 Self::Cut => "Cut",
567 Self::Paste => "Paste",
568
569 Self::Space => "Space",
570 Self::Colon => "Colon",
571 Self::Comma => "Comma",
572 Self::Minus => "Minus",
573 Self::Period => "Period",
574 Self::Plus => "Plus",
575 Self::Equals => "Equals",
576 Self::Semicolon => "Semicolon",
577 Self::Backslash => "Backslash",
578 Self::Slash => "Slash",
579 Self::Pipe => "Pipe",
580 Self::Questionmark => "Questionmark",
581 Self::Exclamationmark => "Exclamationmark",
582 Self::OpenBracket => "OpenBracket",
583 Self::CloseBracket => "CloseBracket",
584 Self::OpenCurlyBracket => "OpenCurlyBracket",
585 Self::CloseCurlyBracket => "CloseCurlyBracket",
586 Self::Backtick => "Backtick",
587 Self::Quote => "Quote",
588
589 Self::Num0 => "0",
590 Self::Num1 => "1",
591 Self::Num2 => "2",
592 Self::Num3 => "3",
593 Self::Num4 => "4",
594 Self::Num5 => "5",
595 Self::Num6 => "6",
596 Self::Num7 => "7",
597 Self::Num8 => "8",
598 Self::Num9 => "9",
599
600 Self::A => "A",
601 Self::B => "B",
602 Self::C => "C",
603 Self::D => "D",
604 Self::E => "E",
605 Self::F => "F",
606 Self::G => "G",
607 Self::H => "H",
608 Self::I => "I",
609 Self::J => "J",
610 Self::K => "K",
611 Self::L => "L",
612 Self::M => "M",
613 Self::N => "N",
614 Self::O => "O",
615 Self::P => "P",
616 Self::Q => "Q",
617 Self::R => "R",
618 Self::S => "S",
619 Self::T => "T",
620 Self::U => "U",
621 Self::V => "V",
622 Self::W => "W",
623 Self::X => "X",
624 Self::Y => "Y",
625 Self::Z => "Z",
626 Self::F1 => "F1",
627 Self::F2 => "F2",
628 Self::F3 => "F3",
629 Self::F4 => "F4",
630 Self::F5 => "F5",
631 Self::F6 => "F6",
632 Self::F7 => "F7",
633 Self::F8 => "F8",
634 Self::F9 => "F9",
635 Self::F10 => "F10",
636 Self::F11 => "F11",
637 Self::F12 => "F12",
638 Self::F13 => "F13",
639 Self::F14 => "F14",
640 Self::F15 => "F15",
641 Self::F16 => "F16",
642 Self::F17 => "F17",
643 Self::F18 => "F18",
644 Self::F19 => "F19",
645 Self::F20 => "F20",
646 Self::F21 => "F21",
647 Self::F22 => "F22",
648 Self::F23 => "F23",
649 Self::F24 => "F24",
650 Self::F25 => "F25",
651 Self::F26 => "F26",
652 Self::F27 => "F27",
653 Self::F28 => "F28",
654 Self::F29 => "F29",
655 Self::F30 => "F30",
656 Self::F31 => "F31",
657 Self::F32 => "F32",
658 Self::F33 => "F33",
659 Self::F34 => "F34",
660 Self::F35 => "F35",
661
662 Self::BrowserBack => "BrowserBack",
663
664 Self::ShiftLeft => "ShiftLeft",
665 Self::ShiftRight => "ShiftRight",
666 Self::ControlLeft => "ControlLeft",
667 Self::ControlRight => "ControlRight",
668 Self::AltLeft => "AltLeft",
669 Self::AltRight => "AltRight",
670 Self::SuperLeft => "SuperLeft",
671 Self::SuperRight => "SuperRight",
672
673 Self::IntlBackslash => "IntlBackslash",
674 }
675 }
676}
677
678#[test]
679fn test_key_from_name() {
680 assert_eq!(
681 Key::ALL.len(),
682 Key::IntlBackslash as usize + 1,
683 "Some keys are missing in Key::ALL"
684 );
685
686 for &key in Key::ALL {
687 let name = key.name();
688 assert_eq!(
689 Key::from_name(name),
690 Some(key),
691 "Failed to roundtrip {key:?} from name {name:?}"
692 );
693
694 let symbol = key.symbol_or_name();
695 assert_eq!(
696 Key::from_name(symbol),
697 Some(key),
698 "Failed to roundtrip {key:?} from symbol {symbol:?}"
699 );
700 }
701}