-
Notifications
You must be signed in to change notification settings - Fork 909
/
keyboard.rs
1688 lines (1660 loc) · 66.3 KB
/
keyboard.rs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Types related to the keyboard.
// This file contains a substantial portion of the UI Events Specification by the W3C. In
// particular, the variant names within `Key` and `KeyCode` and their documentation are modified
// versions of contents of the aforementioned specification.
//
// The original documents are:
//
// ### For `Key`
// UI Events KeyboardEvent key Values
// https://www.w3.org/TR/2017/CR-uievents-key-20170601/
// Copyright © 2017 W3C® (MIT, ERCIM, Keio, Beihang).
//
// ### For `KeyCode`
// UI Events KeyboardEvent code Values
// https://www.w3.org/TR/2017/CR-uievents-code-20170601/
// Copyright © 2017 W3C® (MIT, ERCIM, Keio, Beihang).
//
// These documents were used under the terms of the following license. This W3C license as well as
// the W3C short notice apply to the `Key` and `KeyCode` enums and their variants and the
// documentation attached to their variants.
// --------- BEGGINING OF W3C LICENSE --------------------------------------------------------------
//
// License
//
// By obtaining and/or copying this work, you (the licensee) agree that you have read, understood,
// and will comply with the following terms and conditions.
//
// Permission to copy, modify, and distribute this work, with or without modification, for any
// purpose and without fee or royalty is hereby granted, provided that you include the following on
// ALL copies of the work or portions thereof, including modifications:
//
// - The full text of this NOTICE in a location viewable to users of the redistributed or derivative
// work.
// - Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none
// exist, the W3C Software and Document Short Notice should be included.
// - Notice of any changes or modifications, through a copyright statement on the new code or
// document such as "This software or document includes material copied from or derived from
// [title and URI of the W3C document]. Copyright © [YEAR] W3C® (MIT, ERCIM, Keio, Beihang)."
//
// Disclaimers
//
// THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR
// ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD
// PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
//
// COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES
// ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT.
//
// The name and trademarks of copyright holders may NOT be used in advertising or publicity
// pertaining to the work without specific, written prior permission. Title to copyright in this
// work will at all times remain with copyright holders.
//
// --------- END OF W3C LICENSE --------------------------------------------------------------------
// --------- BEGGINING OF W3C SHORT NOTICE ---------------------------------------------------------
//
// winit: https://github.com/rust-windowing/winit
//
// Copyright © 2021 World Wide Web Consortium, (Massachusetts Institute of Technology, European
// Research Consortium for Informatics and Mathematics, Keio University, Beihang). All Rights
// Reserved. This work is distributed under the W3C® Software License [1] in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// [1] http://www.w3.org/Consortium/Legal/copyright-software
//
// --------- END OF W3C SHORT NOTICE ---------------------------------------------------------------
use smol_str::SmolStr;
/// Contains the platform-native physical key identifier
///
/// The exact values vary from platform to platform (which is part of why this is a per-platform
/// enum), but the values are primarily tied to the key's physical location on the keyboard.
///
/// This enum is primarily used to store raw keycodes when Winit doesn't map a given native
/// physical key identifier to a meaningful [`KeyCode`] variant. In the presence of identifiers we
/// haven't mapped for you yet, this lets you use use [`KeyCode`] to:
///
/// - Correctly match key press and release events.
/// - On non-web platforms, support assigning keybinds to virtually any key through a UI.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum NativeKeyCode {
Unidentified,
/// An Android "scancode".
Android(u32),
/// A macOS "scancode".
MacOS(u16),
/// A Windows "scancode".
Windows(u16),
/// An XKB "keycode".
Xkb(u32),
}
impl std::fmt::Debug for NativeKeyCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use NativeKeyCode::{Android, MacOS, Unidentified, Windows, Xkb};
let mut debug_tuple;
match self {
Unidentified => {
debug_tuple = f.debug_tuple("Unidentified");
}
Android(code) => {
debug_tuple = f.debug_tuple("Android");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
MacOS(code) => {
debug_tuple = f.debug_tuple("MacOS");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
Windows(code) => {
debug_tuple = f.debug_tuple("Windows");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
Xkb(code) => {
debug_tuple = f.debug_tuple("Xkb");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
}
debug_tuple.finish()
}
}
/// Contains the platform-native logical key identifier
///
/// Exactly what that means differs from platform to platform, but the values are to some degree
/// tied to the currently active keyboard layout. The same key on the same keyboard may also report
/// different values on different platforms, which is one of the reasons this is a per-platform
/// enum.
///
/// This enum is primarily used to store raw keysym when Winit doesn't map a given native logical
/// key identifier to a meaningful [`Key`] variant. This lets you use [`Key`], and let the user
/// define keybinds which work in the presence of identifiers we haven't mapped for you yet.
#[derive(Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum NativeKey {
Unidentified,
/// An Android "keycode", which is similar to a "virtual-key code" on Windows.
Android(u32),
/// A macOS "scancode". There does not appear to be any direct analogue to either keysyms or
/// "virtual-key" codes in macOS, so we report the scancode instead.
MacOS(u16),
/// A Windows "virtual-key code".
Windows(u16),
/// An XKB "keysym".
Xkb(u32),
/// A "key value string".
Web(SmolStr),
}
impl std::fmt::Debug for NativeKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use NativeKey::{Android, MacOS, Unidentified, Web, Windows, Xkb};
let mut debug_tuple;
match self {
Unidentified => {
debug_tuple = f.debug_tuple("Unidentified");
}
Android(code) => {
debug_tuple = f.debug_tuple("Android");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
MacOS(code) => {
debug_tuple = f.debug_tuple("MacOS");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
Windows(code) => {
debug_tuple = f.debug_tuple("Windows");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
Xkb(code) => {
debug_tuple = f.debug_tuple("Xkb");
debug_tuple.field(&format_args!("0x{code:04X}"));
}
Web(code) => {
debug_tuple = f.debug_tuple("Web");
debug_tuple.field(code);
}
}
debug_tuple.finish()
}
}
/// Represents the location of a physical key.
///
/// This mostly conforms to the UI Events Specification's [`KeyboardEvent.code`] with a few
/// exceptions:
/// - The keys that the specification calls "MetaLeft" and "MetaRight" are named "SuperLeft" and
/// "SuperRight" here.
/// - The key that the specification calls "Super" is reported as `Unidentified` here.
/// - The `Unidentified` variant here, can still identify a key through it's `NativeKeyCode`.
///
/// [`KeyboardEvent.code`]: https://w3c.github.io/uievents-code/#code-value-tables
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyCode {
/// This variant is used when the key cannot be translated to any other variant.
///
/// The native keycode is provided (if available) so you're able to more reliably match
/// key-press and key-release events by hashing the [`KeyCode`]. It is also possible to use
/// this for keybinds for non-standard keys, but such keybinds are tied to a given platform.
Unidentified(NativeKeyCode),
/// <kbd>`</kbd> on a US keyboard. This is also called a backtick or grave.
/// This is the <kbd>半角</kbd>/<kbd>全角</kbd>/<kbd>漢字</kbd>
/// (hankaku/zenkaku/kanji) key on Japanese keyboards
Backquote,
/// Used for both the US <kbd>\\</kbd> (on the 101-key layout) and also for the key
/// located between the <kbd>"</kbd> and <kbd>Enter</kbd> keys on row C of the 102-,
/// 104- and 106-key layouts.
/// Labeled <kbd>#</kbd> on a UK (102) keyboard.
Backslash,
/// <kbd>[</kbd> on a US keyboard.
BracketLeft,
/// <kbd>]</kbd> on a US keyboard.
BracketRight,
/// <kbd>,</kbd> on a US keyboard.
Comma,
/// <kbd>0</kbd> on a US keyboard.
Digit0,
/// <kbd>1</kbd> on a US keyboard.
Digit1,
/// <kbd>2</kbd> on a US keyboard.
Digit2,
/// <kbd>3</kbd> on a US keyboard.
Digit3,
/// <kbd>4</kbd> on a US keyboard.
Digit4,
/// <kbd>5</kbd> on a US keyboard.
Digit5,
/// <kbd>6</kbd> on a US keyboard.
Digit6,
/// <kbd>7</kbd> on a US keyboard.
Digit7,
/// <kbd>8</kbd> on a US keyboard.
Digit8,
/// <kbd>9</kbd> on a US keyboard.
Digit9,
/// <kbd>=</kbd> on a US keyboard.
Equal,
/// Located between the left <kbd>Shift</kbd> and <kbd>Z</kbd> keys.
/// Labeled <kbd>\\</kbd> on a UK keyboard.
IntlBackslash,
/// Located between the <kbd>/</kbd> and right <kbd>Shift</kbd> keys.
/// Labeled <kbd>\\</kbd> (ro) on a Japanese keyboard.
IntlRo,
/// Located between the <kbd>=</kbd> and <kbd>Backspace</kbd> keys.
/// Labeled <kbd>¥</kbd> (yen) on a Japanese keyboard. <kbd>\\</kbd> on a
/// Russian keyboard.
IntlYen,
/// <kbd>a</kbd> on a US keyboard.
/// Labeled <kbd>q</kbd> on an AZERTY (e.g., French) keyboard.
KeyA,
/// <kbd>b</kbd> on a US keyboard.
KeyB,
/// <kbd>c</kbd> on a US keyboard.
KeyC,
/// <kbd>d</kbd> on a US keyboard.
KeyD,
/// <kbd>e</kbd> on a US keyboard.
KeyE,
/// <kbd>f</kbd> on a US keyboard.
KeyF,
/// <kbd>g</kbd> on a US keyboard.
KeyG,
/// <kbd>h</kbd> on a US keyboard.
KeyH,
/// <kbd>i</kbd> on a US keyboard.
KeyI,
/// <kbd>j</kbd> on a US keyboard.
KeyJ,
/// <kbd>k</kbd> on a US keyboard.
KeyK,
/// <kbd>l</kbd> on a US keyboard.
KeyL,
/// <kbd>m</kbd> on a US keyboard.
KeyM,
/// <kbd>n</kbd> on a US keyboard.
KeyN,
/// <kbd>o</kbd> on a US keyboard.
KeyO,
/// <kbd>p</kbd> on a US keyboard.
KeyP,
/// <kbd>q</kbd> on a US keyboard.
/// Labeled <kbd>a</kbd> on an AZERTY (e.g., French) keyboard.
KeyQ,
/// <kbd>r</kbd> on a US keyboard.
KeyR,
/// <kbd>s</kbd> on a US keyboard.
KeyS,
/// <kbd>t</kbd> on a US keyboard.
KeyT,
/// <kbd>u</kbd> on a US keyboard.
KeyU,
/// <kbd>v</kbd> on a US keyboard.
KeyV,
/// <kbd>w</kbd> on a US keyboard.
/// Labeled <kbd>z</kbd> on an AZERTY (e.g., French) keyboard.
KeyW,
/// <kbd>x</kbd> on a US keyboard.
KeyX,
/// <kbd>y</kbd> on a US keyboard.
/// Labeled <kbd>z</kbd> on a QWERTZ (e.g., German) keyboard.
KeyY,
/// <kbd>z</kbd> on a US keyboard.
/// Labeled <kbd>w</kbd> on an AZERTY (e.g., French) keyboard, and <kbd>y</kbd> on a
/// QWERTZ (e.g., German) keyboard.
KeyZ,
/// <kbd>-</kbd> on a US keyboard.
Minus,
/// <kbd>.</kbd> on a US keyboard.
Period,
/// <kbd>'</kbd> on a US keyboard.
Quote,
/// <kbd>;</kbd> on a US keyboard.
Semicolon,
/// <kbd>/</kbd> on a US keyboard.
Slash,
/// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
AltLeft,
/// <kbd>Alt</kbd>, <kbd>Option</kbd>, or <kbd>⌥</kbd>.
/// This is labeled <kbd>AltGr</kbd> on many keyboard layouts.
AltRight,
/// <kbd>Backspace</kbd> or <kbd>⌫</kbd>.
/// Labeled <kbd>Delete</kbd> on Apple keyboards.
Backspace,
/// <kbd>CapsLock</kbd> or <kbd>⇪</kbd>
CapsLock,
/// The application context menu key, which is typically found between the right
/// <kbd>Super</kbd> key and the right <kbd>Control</kbd> key.
ContextMenu,
/// <kbd>Control</kbd> or <kbd>⌃</kbd>
ControlLeft,
/// <kbd>Control</kbd> or <kbd>⌃</kbd>
ControlRight,
/// <kbd>Enter</kbd> or <kbd>↵</kbd>. Labeled <kbd>Return</kbd> on Apple keyboards.
Enter,
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
SuperLeft,
/// The Windows, <kbd>⌘</kbd>, <kbd>Command</kbd>, or other OS symbol key.
SuperRight,
/// <kbd>Shift</kbd> or <kbd>⇧</kbd>
ShiftLeft,
/// <kbd>Shift</kbd> or <kbd>⇧</kbd>
ShiftRight,
/// <kbd> </kbd> (space)
Space,
/// <kbd>Tab</kbd> or <kbd>⇥</kbd>
Tab,
/// Japanese: <kbd>変</kbd> (henkan)
Convert,
/// Japanese: <kbd>カタカナ</kbd>/<kbd>ひらがな</kbd>/<kbd>ローマ字</kbd> (katakana/hiragana/romaji)
KanaMode,
/// Korean: HangulMode <kbd>한/영</kbd> (han/yeong)
///
/// Japanese (Mac keyboard): <kbd>か</kbd> (kana)
Lang1,
/// Korean: Hanja <kbd>한</kbd> (hanja)
///
/// Japanese (Mac keyboard): <kbd>英</kbd> (eisu)
Lang2,
/// Japanese (word-processing keyboard): Katakana
Lang3,
/// Japanese (word-processing keyboard): Hiragana
Lang4,
/// Japanese (word-processing keyboard): Zenkaku/Hankaku
Lang5,
/// Japanese: <kbd>無変換</kbd> (muhenkan)
NonConvert,
/// <kbd>⌦</kbd>. The forward delete key.
/// Note that on Apple keyboards, the key labelled <kbd>Delete</kbd> on the main part of
/// the keyboard is encoded as [`Backspace`].
///
/// [`Backspace`]: Self::Backspace
Delete,
/// <kbd>Page Down</kbd>, <kbd>End</kbd>, or <kbd>↘</kbd>
End,
/// <kbd>Help</kbd>. Not present on standard PC keyboards.
Help,
/// <kbd>Home</kbd> or <kbd>↖</kbd>
Home,
/// <kbd>Insert</kbd> or <kbd>Ins</kbd>. Not present on Apple keyboards.
Insert,
/// <kbd>Page Down</kbd>, <kbd>PgDn</kbd>, or <kbd>⇟</kbd>
PageDown,
/// <kbd>Page Up</kbd>, <kbd>PgUp</kbd>, or <kbd>⇞</kbd>
PageUp,
/// <kbd>↓</kbd>
ArrowDown,
/// <kbd>←</kbd>
ArrowLeft,
/// <kbd>→</kbd>
ArrowRight,
/// <kbd>↑</kbd>
ArrowUp,
/// On the Mac, this is used for the numpad <kbd>Clear</kbd> key.
NumLock,
/// <kbd>0 Ins</kbd> on a keyboard. <kbd>0</kbd> on a phone or remote control
Numpad0,
/// <kbd>1 End</kbd> on a keyboard. <kbd>1</kbd> or <kbd>1 QZ</kbd> on a phone or remote control
Numpad1,
/// <kbd>2 ↓</kbd> on a keyboard. <kbd>2 ABC</kbd> on a phone or remote control
Numpad2,
/// <kbd>3 PgDn</kbd> on a keyboard. <kbd>3 DEF</kbd> on a phone or remote control
Numpad3,
/// <kbd>4 ←</kbd> on a keyboard. <kbd>4 GHI</kbd> on a phone or remote control
Numpad4,
/// <kbd>5</kbd> on a keyboard. <kbd>5 JKL</kbd> on a phone or remote control
Numpad5,
/// <kbd>6 →</kbd> on a keyboard. <kbd>6 MNO</kbd> on a phone or remote control
Numpad6,
/// <kbd>7 Home</kbd> on a keyboard. <kbd>7 PQRS</kbd> or <kbd>7 PRS</kbd> on a phone
/// or remote control
Numpad7,
/// <kbd>8 ↑</kbd> on a keyboard. <kbd>8 TUV</kbd> on a phone or remote control
Numpad8,
/// <kbd>9 PgUp</kbd> on a keyboard. <kbd>9 WXYZ</kbd> or <kbd>9 WXY</kbd> on a phone
/// or remote control
Numpad9,
/// <kbd>+</kbd>
NumpadAdd,
/// Found on the Microsoft Natural Keyboard.
NumpadBackspace,
/// <kbd>C</kbd> or <kbd>A</kbd> (All Clear). Also for use with numpads that have a
/// <kbd>Clear</kbd> key that is separate from the <kbd>NumLock</kbd> key. On the Mac, the
/// numpad <kbd>Clear</kbd> key is encoded as [`NumLock`].
///
/// [`NumLock`]: Self::NumLock
NumpadClear,
/// <kbd>C</kbd> (Clear Entry)
NumpadClearEntry,
/// <kbd>,</kbd> (thousands separator). For locales where the thousands separator
/// is a "." (e.g., Brazil), this key may generate a <kbd>.</kbd>.
NumpadComma,
/// <kbd>. Del</kbd>. For locales where the decimal separator is "," (e.g.,
/// Brazil), this key may generate a <kbd>,</kbd>.
NumpadDecimal,
/// <kbd>/</kbd>
NumpadDivide,
NumpadEnter,
/// <kbd>=</kbd>
NumpadEqual,
/// <kbd>#</kbd> on a phone or remote control device. This key is typically found
/// below the <kbd>9</kbd> key and to the right of the <kbd>0</kbd> key.
NumpadHash,
/// <kbd>M</kbd> Add current entry to the value stored in memory.
NumpadMemoryAdd,
/// <kbd>M</kbd> Clear the value stored in memory.
NumpadMemoryClear,
/// <kbd>M</kbd> Replace the current entry with the value stored in memory.
NumpadMemoryRecall,
/// <kbd>M</kbd> Replace the value stored in memory with the current entry.
NumpadMemoryStore,
/// <kbd>M</kbd> Subtract current entry from the value stored in memory.
NumpadMemorySubtract,
/// <kbd>*</kbd> on a keyboard. For use with numpads that provide mathematical
/// operations (<kbd>+</kbd>, <kbd>-</kbd> <kbd>*</kbd> and <kbd>/</kbd>).
///
/// Use `NumpadStar` for the <kbd>*</kbd> key on phones and remote controls.
NumpadMultiply,
/// <kbd>(</kbd> Found on the Microsoft Natural Keyboard.
NumpadParenLeft,
/// <kbd>)</kbd> Found on the Microsoft Natural Keyboard.
NumpadParenRight,
/// <kbd>*</kbd> on a phone or remote control device.
///
/// This key is typically found below the <kbd>7</kbd> key and to the left of
/// the <kbd>0</kbd> key.
///
/// Use <kbd>"NumpadMultiply"</kbd> for the <kbd>*</kbd> key on
/// numeric keypads.
NumpadStar,
/// <kbd>-</kbd>
NumpadSubtract,
/// <kbd>Esc</kbd> or <kbd>⎋</kbd>
Escape,
/// <kbd>Fn</kbd> This is typically a hardware key that does not generate a separate code.
Fn,
/// <kbd>FLock</kbd> or <kbd>FnLock</kbd>. Function Lock key. Found on the Microsoft
/// Natural Keyboard.
FnLock,
/// <kbd>PrtScr SysRq</kbd> or <kbd>Print Screen</kbd>
PrintScreen,
/// <kbd>Scroll Lock</kbd>
ScrollLock,
/// <kbd>Pause Break</kbd>
Pause,
/// Some laptops place this key to the left of the <kbd>↑</kbd> key.
///
/// This also the "back" button (triangle) on Android.
BrowserBack,
BrowserFavorites,
/// Some laptops place this key to the right of the <kbd>↑</kbd> key.
BrowserForward,
/// The "home" button on Android.
BrowserHome,
BrowserRefresh,
BrowserSearch,
BrowserStop,
/// <kbd>Eject</kbd> or <kbd>⏏</kbd>. This key is placed in the function section on some Apple
/// keyboards.
Eject,
/// Sometimes labelled <kbd>My Computer</kbd> on the keyboard
LaunchApp1,
/// Sometimes labelled <kbd>Calculator</kbd> on the keyboard
LaunchApp2,
LaunchMail,
MediaPlayPause,
MediaSelect,
MediaStop,
MediaTrackNext,
MediaTrackPrevious,
/// This key is placed in the function section on some Apple keyboards, replacing the
/// <kbd>Eject</kbd> key.
Power,
Sleep,
AudioVolumeDown,
AudioVolumeMute,
AudioVolumeUp,
WakeUp,
// Legacy modifier key. Also called "Super" in certain places.
Meta,
// Legacy modifier key.
Hyper,
Turbo,
Abort,
Resume,
Suspend,
/// Found on Sun’s USB keyboard.
Again,
/// Found on Sun’s USB keyboard.
Copy,
/// Found on Sun’s USB keyboard.
Cut,
/// Found on Sun’s USB keyboard.
Find,
/// Found on Sun’s USB keyboard.
Open,
/// Found on Sun’s USB keyboard.
Paste,
/// Found on Sun’s USB keyboard.
Props,
/// Found on Sun’s USB keyboard.
Select,
/// Found on Sun’s USB keyboard.
Undo,
/// Use for dedicated <kbd>ひらがな</kbd> key found on some Japanese word processing keyboards.
Hiragana,
/// Use for dedicated <kbd>カタカナ</kbd> key found on some Japanese word processing keyboards.
Katakana,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F1,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F2,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F3,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F4,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F5,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F6,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F7,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F8,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F9,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F10,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F11,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F12,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F13,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F14,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F15,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F16,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F17,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F18,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F19,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F20,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F21,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F22,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F23,
/// General-purpose function key.
/// Usually found at the top of the keyboard.
F24,
/// General-purpose function key.
F25,
/// General-purpose function key.
F26,
/// General-purpose function key.
F27,
/// General-purpose function key.
F28,
/// General-purpose function key.
F29,
/// General-purpose function key.
F30,
/// General-purpose function key.
F31,
/// General-purpose function key.
F32,
/// General-purpose function key.
F33,
/// General-purpose function key.
F34,
/// General-purpose function key.
F35,
}
/// Key represents the meaning of a keypress.
///
/// This mostly conforms to the UI Events Specification's [`KeyboardEvent.key`] with a few
/// exceptions:
/// - The `Super` variant here, is named `Meta` in the aforementioned specification. (There's
/// another key which the specification calls `Super`. That does not exist here.)
/// - The `Space` variant here, can be identified by the character it generates in the
/// specificaiton.
/// - The `Unidentified` variant here, can still identifiy a key through it's `NativeKeyCode`.
/// - The `Dead` variant here, can specify the character which is inserted when pressing the
/// dead-key twice.
///
/// [`KeyboardEvent.key`]: https://w3c.github.io/uievents-key/
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Key<Str = SmolStr> {
/// A key string that corresponds to the character typed by the user, taking into account the
/// user’s current locale setting, and any system-level keyboard mapping overrides that are in
/// effect.
Character(Str),
/// This variant is used when the key cannot be translated to any other variant.
///
/// The native key is provided (if available) in order to allow the user to specify keybindings
/// for keys which are not defined by this API, mainly through some sort of UI.
Unidentified(NativeKey),
/// Contains the text representation of the dead-key when available.
///
/// ## Platform-specific
/// - **Web:** Always contains `None`
Dead(Option<char>),
/// The `Alt` (Alternative) key.
///
/// This key enables the alternate modifier function for interpreting concurrent or subsequent
/// keyboard input. This key value is also used for the Apple <kbd>Option</kbd> key.
Alt,
/// The Alternate Graphics (<kbd>AltGr</kbd> or <kbd>AltGraph</kbd>) key.
///
/// This key is used enable the ISO Level 3 shift modifier (the standard `Shift` key is the
/// level 2 modifier).
AltGraph,
/// The `Caps Lock` (Capital) key.
///
/// Toggle capital character lock function for interpreting subsequent keyboard input event.
CapsLock,
/// The `Control` or `Ctrl` key.
///
/// Used to enable control modifier function for interpreting concurrent or subsequent keyboard
/// input.
Control,
/// The Function switch `Fn` key. Activating this key simultaneously with another key changes
/// that key’s value to an alternate character or function. This key is often handled directly
/// in the keyboard hardware and does not usually generate key events.
Fn,
/// The Function-Lock (`FnLock` or `F-Lock`) key. Activating this key switches the mode of the
/// keyboard to changes some keys' values to an alternate character or function. This key is
/// often handled directly in the keyboard hardware and does not usually generate key events.
FnLock,
/// The `NumLock` or Number Lock key. Used to toggle numpad mode function for interpreting
/// subsequent keyboard input.
NumLock,
/// Toggle between scrolling and cursor movement modes.
ScrollLock,
/// Used to enable shift modifier function for interpreting concurrent or subsequent keyboard
/// input.
Shift,
/// The Symbol modifier key (used on some virtual keyboards).
Symbol,
SymbolLock,
// Legacy modifier key. Also called "Super" in certain places.
Meta,
// Legacy modifier key.
Hyper,
/// Used to enable "super" modifier function for interpreting concurrent or subsequent keyboard
/// input. This key value is used for the "Windows Logo" key and the Apple `Command` or `⌘` key.
///
/// Note: In some contexts (e.g. the Web) this is referred to as the "Meta" key.
Super,
/// The `Enter` or `↵` key. Used to activate current selection or accept current input. This key
/// value is also used for the `Return` (Macintosh numpad) key. This key value is also used for
/// the Android `KEYCODE_DPAD_CENTER`.
Enter,
/// The Horizontal Tabulation `Tab` key.
Tab,
/// Used in text to insert a space between words. Usually located below the character keys.
Space,
/// Navigate or traverse downward. (`KEYCODE_DPAD_DOWN`)
ArrowDown,
/// Navigate or traverse leftward. (`KEYCODE_DPAD_LEFT`)
ArrowLeft,
/// Navigate or traverse rightward. (`KEYCODE_DPAD_RIGHT`)
ArrowRight,
/// Navigate or traverse upward. (`KEYCODE_DPAD_UP`)
ArrowUp,
/// The End key, used with keyboard entry to go to the end of content (`KEYCODE_MOVE_END`).
End,
/// The Home key, used with keyboard entry, to go to start of content (`KEYCODE_MOVE_HOME`).
/// For the mobile phone `Home` key (which goes to the phone’s main screen), use [`GoHome`].
///
/// [`GoHome`]: Self::GoHome
Home,
/// Scroll down or display next page of content.
PageDown,
/// Scroll up or display previous page of content.
PageUp,
/// Used to remove the character to the left of the cursor. This key value is also used for
/// the key labeled `Delete` on MacOS keyboards.
Backspace,
/// Remove the currently selected input.
Clear,
/// Copy the current selection. (`APPCOMMAND_COPY`)
Copy,
/// The Cursor Select key.
CrSel,
/// Cut the current selection. (`APPCOMMAND_CUT`)
Cut,
/// Used to delete the character to the right of the cursor. This key value is also used for the
/// key labeled `Delete` on MacOS keyboards when `Fn` is active.
Delete,
/// The Erase to End of Field key. This key deletes all characters from the current cursor
/// position to the end of the current field.
EraseEof,
/// The Extend Selection (Exsel) key.
ExSel,
/// Toggle between text modes for insertion or overtyping.
/// (`KEYCODE_INSERT`)
Insert,
/// The Paste key. (`APPCOMMAND_PASTE`)
Paste,
/// Redo the last action. (`APPCOMMAND_REDO`)
Redo,
/// Undo the last action. (`APPCOMMAND_UNDO`)
Undo,
/// The Accept (Commit, OK) key. Accept current option or input method sequence conversion.
Accept,
/// Redo or repeat an action.
Again,
/// The Attention (Attn) key.
Attn,
Cancel,
/// Show the application’s context menu.
/// This key is commonly found between the right `Super` key and the right `Control` key.
ContextMenu,
/// The `Esc` key. This key was originally used to initiate an escape sequence, but is
/// now more generally used to exit or "escape" the current context, such as closing a dialog
/// or exiting full screen mode.
Escape,
Execute,
/// Open the Find dialog. (`APPCOMMAND_FIND`)
Find,
/// Open a help dialog or toggle display of help information. (`APPCOMMAND_HELP`,
/// `KEYCODE_HELP`)
Help,
/// Pause the current state or application (as appropriate).
///
/// Note: Do not use this value for the `Pause` button on media controllers. Use `"MediaPause"`
/// instead.
Pause,
/// Play or resume the current state or application (as appropriate).
///
/// Note: Do not use this value for the `Play` button on media controllers. Use `"MediaPlay"`
/// instead.
Play,
/// The properties (Props) key.
Props,
Select,
/// The ZoomIn key. (`KEYCODE_ZOOM_IN`)
ZoomIn,
/// The ZoomOut key. (`KEYCODE_ZOOM_OUT`)
ZoomOut,
/// The Brightness Down key. Typically controls the display brightness.
/// (`KEYCODE_BRIGHTNESS_DOWN`)
BrightnessDown,
/// The Brightness Up key. Typically controls the display brightness. (`KEYCODE_BRIGHTNESS_UP`)
BrightnessUp,
/// Toggle removable media to eject (open) and insert (close) state. (`KEYCODE_MEDIA_EJECT`)
Eject,
LogOff,
/// Toggle power state. (`KEYCODE_POWER`)
/// Note: Note: Some devices might not expose this key to the operating environment.
Power,
/// The `PowerOff` key. Sometime called `PowerDown`.
PowerOff,
/// Initiate print-screen function.
PrintScreen,
/// The Hibernate key. This key saves the current state of the computer to disk so that it can
/// be restored. The computer will then shutdown.
Hibernate,
/// The Standby key. This key turns off the display and places the computer into a low-power
/// mode without completely shutting down. It is sometimes labelled `Suspend` or `Sleep` key.
/// (`KEYCODE_SLEEP`)
Standby,
/// The WakeUp key. (`KEYCODE_WAKEUP`)
WakeUp,
/// Initate the multi-candidate mode.
AllCandidates,
Alphanumeric,
/// Initiate the Code Input mode to allow characters to be entered by
/// their code points.
CodeInput,
/// The Compose key, also known as "Multi_key" on the X Window System. This key acts in a
/// manner similar to a dead key, triggering a mode where subsequent key presses are combined to
/// produce a different character.
Compose,
/// Convert the current input method sequence.
Convert,
/// The Final Mode `Final` key used on some Asian keyboards, to enable the final mode for IMEs.
FinalMode,
/// Switch to the first character group. (ISO/IEC 9995)
GroupFirst,
/// Switch to the last character group. (ISO/IEC 9995)
GroupLast,
/// Switch to the next character group. (ISO/IEC 9995)
GroupNext,
/// Switch to the previous character group. (ISO/IEC 9995)
GroupPrevious,
/// Toggle between or cycle through input modes of IMEs.
ModeChange,
NextCandidate,
/// Accept current input method sequence without
/// conversion in IMEs.
NonConvert,
PreviousCandidate,
Process,
SingleCandidate,
/// Toggle between Hangul and English modes.
HangulMode,
HanjaMode,
JunjaMode,
/// The Eisu key. This key may close the IME, but its purpose is defined by the current IME.
/// (`KEYCODE_EISU`)
Eisu,
/// The (Half-Width) Characters key.
Hankaku,
/// The Hiragana (Japanese Kana characters) key.
Hiragana,
/// The Hiragana/Katakana toggle key. (`KEYCODE_KATAKANA_HIRAGANA`)
HiraganaKatakana,
/// The Kana Mode (Kana Lock) key. This key is used to enter hiragana mode (typically from
/// romaji mode).
KanaMode,
/// The Kanji (Japanese name for ideographic characters of Chinese origin) Mode key. This key is
/// typically used to switch to a hiragana keyboard for the purpose of converting input into
/// kanji. (`KEYCODE_KANA`)
KanjiMode,
/// The Katakana (Japanese Kana characters) key.
Katakana,
/// The Roman characters function key.
Romaji,
/// The Zenkaku (Full-Width) Characters key.
Zenkaku,
/// The Zenkaku/Hankaku (full-width/half-width) toggle key. (`KEYCODE_ZENKAKU_HANKAKU`)
ZenkakuHankaku,
/// General purpose virtual function key, as index 1.
Soft1,
/// General purpose virtual function key, as index 2.
Soft2,
/// General purpose virtual function key, as index 3.
Soft3,
/// General purpose virtual function key, as index 4.
Soft4,
/// Select next (numerically or logically) lower channel. (`APPCOMMAND_MEDIA_CHANNEL_DOWN`,
/// `KEYCODE_CHANNEL_DOWN`)
ChannelDown,
/// Select next (numerically or logically) higher channel. (`APPCOMMAND_MEDIA_CHANNEL_UP`,
/// `KEYCODE_CHANNEL_UP`)
ChannelUp,
/// Close the current document or message (Note: This doesn’t close the application).
/// (`APPCOMMAND_CLOSE`)
Close,
/// Open an editor to forward the current message. (`APPCOMMAND_FORWARD_MAIL`)
MailForward,
/// Open an editor to reply to the current message. (`APPCOMMAND_REPLY_TO_MAIL`)
MailReply,
/// Send the current message. (`APPCOMMAND_SEND_MAIL`)
MailSend,
/// Close the current media, for example to close a CD or DVD tray. (`KEYCODE_MEDIA_CLOSE`)
MediaClose,
/// Initiate or continue forward playback at faster than normal speed, or increase speed if
/// already fast forwarding. (`APPCOMMAND_MEDIA_FAST_FORWARD`, `KEYCODE_MEDIA_FAST_FORWARD`)
MediaFastForward,
/// Pause the currently playing media. (`APPCOMMAND_MEDIA_PAUSE`, `KEYCODE_MEDIA_PAUSE`)
///
/// Note: Media controller devices should use this value rather than `"Pause"` for their pause
/// keys.
MediaPause,
/// Initiate or continue media playback at normal speed, if not currently playing at normal
/// speed. (`APPCOMMAND_MEDIA_PLAY`, `KEYCODE_MEDIA_PLAY`)
MediaPlay,
/// Toggle media between play and pause states. (`APPCOMMAND_MEDIA_PLAY_PAUSE`,
/// `KEYCODE_MEDIA_PLAY_PAUSE`)
MediaPlayPause,
/// Initiate or resume recording of currently selected media. (`APPCOMMAND_MEDIA_RECORD`,
/// `KEYCODE_MEDIA_RECORD`)
MediaRecord,
/// Initiate or continue reverse playback at faster than normal speed, or increase speed if
/// already rewinding. (`APPCOMMAND_MEDIA_REWIND`, `KEYCODE_MEDIA_REWIND`)
MediaRewind,
/// Stop media playing, pausing, forwarding, rewinding, or recording, if not already stopped.
/// (`APPCOMMAND_MEDIA_STOP`, `KEYCODE_MEDIA_STOP`)
MediaStop,
/// Seek to next media or program track. (`APPCOMMAND_MEDIA_NEXTTRACK`, `KEYCODE_MEDIA_NEXT`)
MediaTrackNext,
/// Seek to previous media or program track. (`APPCOMMAND_MEDIA_PREVIOUSTRACK`,
/// `KEYCODE_MEDIA_PREVIOUS`)
MediaTrackPrevious,
/// Open a new document or message. (`APPCOMMAND_NEW`)
New,
/// Open an existing document or message. (`APPCOMMAND_OPEN`)
Open,
/// Print the current document or message. (`APPCOMMAND_PRINT`)
Print,
/// Save the current document or message. (`APPCOMMAND_SAVE`)
Save,
/// Spellcheck the current document or selection. (`APPCOMMAND_SPELL_CHECK`)
SpellCheck,
/// The `11` key found on media numpads that
/// have buttons from `1` ... `12`.
Key11,
/// The `12` key found on media numpads that
/// have buttons from `1` ... `12`.
Key12,
/// Adjust audio balance leftward. (`VK_AUDIO_BALANCE_LEFT`)
AudioBalanceLeft,
/// Adjust audio balance rightward. (`VK_AUDIO_BALANCE_RIGHT`)
AudioBalanceRight,
/// Decrease audio bass boost or cycle down through bass boost states. (`APPCOMMAND_BASS_DOWN`,
/// `VK_BASS_BOOST_DOWN`)
AudioBassBoostDown,
/// Toggle bass boost on/off. (`APPCOMMAND_BASS_BOOST`)
AudioBassBoostToggle,
/// Increase audio bass boost or cycle up through bass boost states. (`APPCOMMAND_BASS_UP`,
/// `VK_BASS_BOOST_UP`)
AudioBassBoostUp,
/// Adjust audio fader towards front. (`VK_FADER_FRONT`)
AudioFaderFront,
/// Adjust audio fader towards rear. (`VK_FADER_REAR`)
AudioFaderRear,
/// Advance surround audio mode to next available mode. (`VK_SURROUND_MODE_NEXT`)
AudioSurroundModeNext,
/// Decrease treble. (`APPCOMMAND_TREBLE_DOWN`)
AudioTrebleDown,
/// Increase treble. (`APPCOMMAND_TREBLE_UP`)
AudioTrebleUp,
/// Decrease audio volume. (`APPCOMMAND_VOLUME_DOWN`, `KEYCODE_VOLUME_DOWN`)
AudioVolumeDown,
/// Increase audio volume. (`APPCOMMAND_VOLUME_UP`, `KEYCODE_VOLUME_UP`)