input/
keyboard.rs

1//! Back-end agnostic keyboard keys.
2
3use std::default::Default;
4
5use crate::{Button, GenericEvent};
6
7// Defining every combination to allow assignment in static expressions.
8bitflags!(
9    #[allow(missing_docs)]
10    #[derive(Deserialize, Serialize)]
11    pub struct ModifierKey: u8 {
12        /// No modifier.
13        const NO_MODIFIER           = 0b0000_0000;
14        /// Ctrl.
15        const CTRL                  = 0b0000_0001;
16        /// Shift.
17        const SHIFT                 = 0b0000_0010;
18        /// Alt.
19        const ALT                   = 0b0000_0100;
20        /// Gui.
21        const GUI                   = 0b0000_1000;
22        /// Ctrl + Shift.
23        const CTRL_SHIFT            = ModifierKey::CTRL.bits
24                                    | ModifierKey::SHIFT.bits;
25        /// Ctrl + Alt.
26        const CTRL_ALT              = ModifierKey::CTRL.bits
27                                    | ModifierKey::ALT.bits;
28        /// Ctrl + Gui.
29        const CTRL_GUI              = ModifierKey::CTRL.bits
30                                    | ModifierKey::GUI.bits;
31        /// Ctrl + Shift + Alt.
32        const CTRL_SHIFT_ALT        = ModifierKey::CTRL.bits
33                                    | ModifierKey::SHIFT.bits
34                                    | ModifierKey::ALT.bits;
35        /// Ctrl + Shift + Gui.
36        const CTRL_SHIFT_GUI        = ModifierKey::CTRL.bits
37                                    | ModifierKey::SHIFT.bits
38                                    | ModifierKey::GUI.bits;
39        /// Ctrl + Shift + Alt + Gui.
40        const CTRL_SHIFT_ALT_GUI    = ModifierKey::CTRL.bits
41                                    | ModifierKey::SHIFT.bits
42                                    | ModifierKey::ALT.bits
43                                    | ModifierKey::GUI.bits;
44        /// Shift + Alt.
45        const SHIFT_ALT             = ModifierKey::SHIFT.bits
46                                    | ModifierKey::ALT.bits;
47        /// Shift + Gui.
48        const SHIFT_GUI             = ModifierKey::SHIFT.bits
49                                    | ModifierKey::GUI.bits;
50        /// Shift + Alt + Gui.
51        const SHIFT_ALT_GUI         = ModifierKey::SHIFT.bits
52                                    | ModifierKey::ALT.bits
53                                    | ModifierKey::GUI.bits;
54        /// Alt + Gui.
55        const ALT_GUI               = ModifierKey::ALT.bits
56                                    | ModifierKey::GUI.bits;
57    }
58);
59
60impl ModifierKey {
61    /// Change modifier key state depending on input.
62    ///
63    /// If the left or side button is released, it counts as a release.
64    pub fn event<E: GenericEvent>(&mut self, e: &E) {
65        if let Some(button) = e.press_args() {
66            match button {
67                Button::Keyboard(Key::LCtrl | Key::RCtrl) => self.insert(ModifierKey::CTRL),
68                Button::Keyboard(Key::LShift | Key::RShift) => self.insert(ModifierKey::SHIFT),
69                Button::Keyboard(Key::LAlt | Key::RAlt) => self.insert(ModifierKey::ALT),
70                Button::Keyboard(Key::LGui | Key::RGui) => self.insert(ModifierKey::GUI),
71                _ => {}
72            }
73        }
74        if let Some(button) = e.release_args() {
75            match button {
76                Button::Keyboard(Key::LCtrl | Key::RCtrl) => self.remove(ModifierKey::CTRL),
77                Button::Keyboard(Key::LShift | Key::RShift) => self.remove(ModifierKey::SHIFT),
78                Button::Keyboard(Key::LAlt | Key::RAlt) => self.remove(ModifierKey::ALT),
79                Button::Keyboard(Key::LGui | Key::RGui) => self.remove(ModifierKey::GUI),
80                _ => {}
81            }
82        }
83        if let Some(false) = e.focus_args() {
84            *self = ModifierKey::NO_MODIFIER;
85        }
86    }
87}
88
89impl Default for ModifierKey {
90    fn default() -> ModifierKey {
91        ModifierKey::NO_MODIFIER
92    }
93}
94
95/// Represent a keyboard key.
96/// Keycodes follows SDL <http://wiki.libsdl.org/SDLKeycodeLookup>
97#[allow(missing_docs)]
98#[derive(Copy, Clone, Deserialize, Serialize, Debug, PartialOrd, PartialEq, Ord, Eq, Hash)]
99pub enum Key {
100    Unknown = 0x00,
101    Backspace = 0x08,
102    Tab = 0x09,
103    Return = 0x0D,
104    Escape = 0x1B,
105    Space = 0x20,
106    Exclaim = 0x21,
107    Quotedbl = 0x22,
108    Hash = 0x23,
109    Dollar = 0x24,
110    Percent = 0x25,
111    Ampersand = 0x26,
112    Quote = 0x27,
113    LeftParen = 0x28,
114    RightParen = 0x29,
115    Asterisk = 0x2A,
116    Plus = 0x2B,
117    Comma = 0x2C,
118    Minus = 0x2D,
119    Period = 0x2E,
120    Slash = 0x2F,
121    D0 = 0x30,
122    D1 = 0x31,
123    D2 = 0x32,
124    D3 = 0x33,
125    D4 = 0x34,
126    D5 = 0x35,
127    D6 = 0x36,
128    D7 = 0x37,
129    D8 = 0x38,
130    D9 = 0x39,
131    Colon = 0x3A,
132    Semicolon = 0x3B,
133    Less = 0x3C,
134    Equals = 0x3D,
135    Greater = 0x3E,
136    Question = 0x3F,
137    At = 0x40,
138    LeftBracket = 0x5B,
139    Backslash = 0x5C,
140    RightBracket = 0x5D,
141    Caret = 0x5E,
142    Underscore = 0x5F,
143    Backquote = 0x60,
144    A = 0x61,
145    B = 0x62,
146    C = 0x63,
147    D = 0x64,
148    E = 0x65,
149    F = 0x66,
150    G = 0x67,
151    H = 0x68,
152    I = 0x69,
153    J = 0x6A,
154    K = 0x6B,
155    L = 0x6C,
156    M = 0x6D,
157    N = 0x6E,
158    O = 0x6F,
159    P = 0x70,
160    Q = 0x71,
161    R = 0x72,
162    S = 0x73,
163    T = 0x74,
164    U = 0x75,
165    V = 0x76,
166    W = 0x77,
167    X = 0x78,
168    Y = 0x79,
169    Z = 0x7A,
170    Delete = 0x7F,
171    CapsLock = 0x4000_0039,
172    F1 = 0x4000_003A,
173    F2 = 0x4000_003B,
174    F3 = 0x4000_003C,
175    F4 = 0x4000_003D,
176    F5 = 0x4000_003E,
177    F6 = 0x4000_003F,
178    F7 = 0x4000_0040,
179    F8 = 0x4000_0041,
180    F9 = 0x4000_0042,
181    F10 = 0x4000_0043,
182    F11 = 0x4000_0044,
183    F12 = 0x4000_0045,
184    PrintScreen = 0x4000_0046,
185    ScrollLock = 0x4000_0047,
186    Pause = 0x4000_0048,
187    Insert = 0x4000_0049,
188    Home = 0x4000_004A,
189    PageUp = 0x4000_004B,
190    End = 0x4000_004D,
191    PageDown = 0x4000_004E,
192    Right = 0x4000_004F,
193    Left = 0x4000_0050,
194    Down = 0x4000_0051,
195    Up = 0x4000_0052,
196    NumLockClear = 0x4000_0053,
197    NumPadDivide = 0x4000_0054,
198    NumPadMultiply = 0x4000_0055,
199    NumPadMinus = 0x4000_0056,
200    NumPadPlus = 0x4000_0057,
201    NumPadEnter = 0x4000_0058,
202    NumPad1 = 0x4000_0059,
203    NumPad2 = 0x4000_005A,
204    NumPad3 = 0x4000_005B,
205    NumPad4 = 0x4000_005C,
206    NumPad5 = 0x4000_005D,
207    NumPad6 = 0x4000_005E,
208    NumPad7 = 0x4000_005F,
209    NumPad8 = 0x4000_0060,
210    NumPad9 = 0x4000_0061,
211    NumPad0 = 0x4000_0062,
212    NumPadPeriod = 0x4000_0063,
213    Application = 0x4000_0065,
214    Power = 0x4000_0066,
215    NumPadEquals = 0x4000_0067,
216    F13 = 0x4000_0068,
217    F14 = 0x4000_0069,
218    F15 = 0x4000_006A,
219    F16 = 0x4000_006B,
220    F17 = 0x4000_006C,
221    F18 = 0x4000_006D,
222    F19 = 0x4000_006E,
223    F20 = 0x4000_006F,
224    F21 = 0x4000_0070,
225    F22 = 0x4000_0071,
226    F23 = 0x4000_0072,
227    F24 = 0x4000_0073,
228    Execute = 0x4000_0074,
229    Help = 0x4000_0075,
230    Menu = 0x4000_0076,
231    Select = 0x4000_0077,
232    Stop = 0x4000_0078,
233    Again = 0x4000_0079,
234    Undo = 0x4000_007A,
235    Cut = 0x4000_007B,
236    Copy = 0x4000_007C,
237    Paste = 0x4000_007D,
238    Find = 0x4000_007E,
239    Mute = 0x4000_007F,
240    VolumeUp = 0x4000_0080,
241    VolumeDown = 0x4000_0081,
242    NumPadComma = 0x4000_0085,
243    NumPadEqualsAS400 = 0x4000_0086,
244    AltErase = 0x4000_0099,
245    Sysreq = 0x4000_009A,
246    Cancel = 0x4000_009B,
247    Clear = 0x4000_009C,
248    Prior = 0x4000_009D,
249    Return2 = 0x4000_009E,
250    Separator = 0x4000_009F,
251    Out = 0x4000_00A0,
252    Oper = 0x4000_00A1,
253    ClearAgain = 0x4000_00A2,
254    CrSel = 0x4000_00A3,
255    ExSel = 0x4000_00A4,
256    NumPad00 = 0x4000_00B0,
257    NumPad000 = 0x4000_00B1,
258    ThousandsSeparator = 0x4000_00B2,
259    DecimalSeparator = 0x4000_00B3,
260    CurrencyUnit = 0x4000_00B4,
261    CurrencySubUnit = 0x4000_00B5,
262    NumPadLeftParen = 0x4000_00B6,
263    NumPadRightParen = 0x4000_00B7,
264    NumPadLeftBrace = 0x4000_00B8,
265    NumPadRightBrace = 0x4000_00B9,
266    NumPadTab = 0x4000_00BA,
267    NumPadBackspace = 0x4000_00BB,
268    NumPadA = 0x4000_00BC,
269    NumPadB = 0x4000_00BD,
270    NumPadC = 0x4000_00BE,
271    NumPadD = 0x4000_00BF,
272    NumPadE = 0x4000_00C0,
273    NumPadF = 0x4000_00C1,
274    NumPadXor = 0x4000_00C2,
275    NumPadPower = 0x4000_00C3,
276    NumPadPercent = 0x4000_00C4,
277    NumPadLess = 0x4000_00C5,
278    NumPadGreater = 0x4000_00C6,
279    NumPadAmpersand = 0x4000_00C7,
280    NumPadDblAmpersand = 0x4000_00C8,
281    NumPadVerticalBar = 0x4000_00C9,
282    NumPadDblVerticalBar = 0x4000_00CA,
283    NumPadColon = 0x4000_00CB,
284    NumPadHash = 0x4000_00CC,
285    NumPadSpace = 0x4000_00CD,
286    NumPadAt = 0x4000_00CE,
287    NumPadExclam = 0x4000_00CF,
288    NumPadMemStore = 0x4000_00D0,
289    NumPadMemRecall = 0x4000_00D1,
290    NumPadMemClear = 0x4000_00D2,
291    NumPadMemAdd = 0x4000_00D3,
292    NumPadMemSubtract = 0x4000_00D4,
293    NumPadMemMultiply = 0x4000_00D5,
294    NumPadMemDivide = 0x4000_00D6,
295    NumPadPlusMinus = 0x4000_00D7,
296    NumPadClear = 0x4000_00D8,
297    NumPadClearEntry = 0x4000_00D9,
298    NumPadBinary = 0x4000_00DA,
299    NumPadOctal = 0x4000_00DB,
300    NumPadDecimal = 0x4000_00DC,
301    NumPadHexadecimal = 0x4000_00DD,
302    LCtrl = 0x4000_00E0,
303    LShift = 0x4000_00E1,
304    LAlt = 0x4000_00E2,
305    LGui = 0x4000_00E3,
306    RCtrl = 0x4000_00E4,
307    RShift = 0x4000_00E5,
308    RAlt = 0x4000_00E6,
309    RGui = 0x4000_00E7,
310    Mode = 0x4000_0101,
311    AudioNext = 0x4000_0102,
312    AudioPrev = 0x4000_0103,
313    AudioStop = 0x4000_0104,
314    AudioPlay = 0x4000_0105,
315    AudioMute = 0x4000_0106,
316    MediaSelect = 0x4000_0107,
317    Www = 0x4000_0108,
318    Mail = 0x4000_0109,
319    Calculator = 0x4000_010A,
320    Computer = 0x4000_010B,
321    AcSearch = 0x4000_010C,
322    AcHome = 0x4000_010D,
323    AcBack = 0x4000_010E,
324    AcForward = 0x4000_010F,
325    AcStop = 0x4000_0110,
326    AcRefresh = 0x4000_0111,
327    AcBookmarks = 0x4000_0112,
328    BrightnessDown = 0x4000_0113,
329    BrightnessUp = 0x4000_0114,
330    DisplaySwitch = 0x4000_0115,
331    KbdIllumToggle = 0x4000_0116,
332    KbdIllumDown = 0x4000_0117,
333    KbdIllumUp = 0x4000_0118,
334    Eject = 0x4000_0119,
335    Sleep = 0x4000_011A,
336}
337
338impl From<u32> for Key {
339    fn from(val: u32) -> Key {
340        match val {
341            0x00 => Key::Unknown,
342            0x08 => Key::Backspace,
343            0x09 => Key::Tab,
344            0x0D => Key::Return,
345            0x1B => Key::Escape,
346            0x20 => Key::Space,
347            0x21 => Key::Exclaim,
348            0x22 => Key::Quotedbl,
349            0x23 => Key::Hash,
350            0x24 => Key::Dollar,
351            0x25 => Key::Percent,
352            0x26 => Key::Ampersand,
353            0x27 => Key::Quote,
354            0x28 => Key::LeftParen,
355            0x29 => Key::RightParen,
356            0x2A => Key::Asterisk,
357            0x2B => Key::Plus,
358            0x2C => Key::Comma,
359            0x2D => Key::Minus,
360            0x2E => Key::Period,
361            0x2F => Key::Slash,
362            0x30 => Key::D0,
363            0x31 => Key::D1,
364            0x32 => Key::D2,
365            0x33 => Key::D3,
366            0x34 => Key::D4,
367            0x35 => Key::D5,
368            0x36 => Key::D6,
369            0x37 => Key::D7,
370            0x38 => Key::D8,
371            0x39 => Key::D9,
372            0x3A => Key::Colon,
373            0x3B => Key::Semicolon,
374            0x3C => Key::Less,
375            0x3D => Key::Equals,
376            0x3E => Key::Greater,
377            0x3F => Key::Question,
378            0x40 => Key::At,
379            0x5B => Key::LeftBracket,
380            0x5C => Key::Backslash,
381            0x5D => Key::RightBracket,
382            0x5E => Key::Caret,
383            0x5F => Key::Underscore,
384            0x60 => Key::Backquote,
385            0x61 => Key::A,
386            0x62 => Key::B,
387            0x63 => Key::C,
388            0x64 => Key::D,
389            0x65 => Key::E,
390            0x66 => Key::F,
391            0x67 => Key::G,
392            0x68 => Key::H,
393            0x69 => Key::I,
394            0x6A => Key::J,
395            0x6B => Key::K,
396            0x6C => Key::L,
397            0x6D => Key::M,
398            0x6E => Key::N,
399            0x6F => Key::O,
400            0x70 => Key::P,
401            0x71 => Key::Q,
402            0x72 => Key::R,
403            0x73 => Key::S,
404            0x74 => Key::T,
405            0x75 => Key::U,
406            0x76 => Key::V,
407            0x77 => Key::W,
408            0x78 => Key::X,
409            0x79 => Key::Y,
410            0x7A => Key::Z,
411            0x7F => Key::Delete,
412            0x4000_0039 => Key::CapsLock,
413            0x4000_003A => Key::F1,
414            0x4000_003B => Key::F2,
415            0x4000_003C => Key::F3,
416            0x4000_003D => Key::F4,
417            0x4000_003E => Key::F5,
418            0x4000_003F => Key::F6,
419            0x4000_0040 => Key::F7,
420            0x4000_0041 => Key::F8,
421            0x4000_0042 => Key::F9,
422            0x4000_0043 => Key::F10,
423            0x4000_0044 => Key::F11,
424            0x4000_0045 => Key::F12,
425            0x4000_0046 => Key::PrintScreen,
426            0x4000_0047 => Key::ScrollLock,
427            0x4000_0048 => Key::Pause,
428            0x4000_0049 => Key::Insert,
429            0x4000_004A => Key::Home,
430            0x4000_004B => Key::PageUp,
431            0x4000_004D => Key::End,
432            0x4000_004E => Key::PageDown,
433            0x4000_004F => Key::Right,
434            0x4000_0050 => Key::Left,
435            0x4000_0051 => Key::Down,
436            0x4000_0052 => Key::Up,
437            0x4000_0053 => Key::NumLockClear,
438            0x4000_0054 => Key::NumPadDivide,
439            0x4000_0055 => Key::NumPadMultiply,
440            0x4000_0056 => Key::NumPadMinus,
441            0x4000_0057 => Key::NumPadPlus,
442            0x4000_0058 => Key::NumPadEnter,
443            0x4000_0059 => Key::NumPad1,
444            0x4000_005A => Key::NumPad2,
445            0x4000_005B => Key::NumPad3,
446            0x4000_005C => Key::NumPad4,
447            0x4000_005D => Key::NumPad5,
448            0x4000_005E => Key::NumPad6,
449            0x4000_005F => Key::NumPad7,
450            0x4000_0060 => Key::NumPad8,
451            0x4000_0061 => Key::NumPad9,
452            0x4000_0062 => Key::NumPad0,
453            0x4000_0063 => Key::NumPadPeriod,
454            0x4000_0065 => Key::Application,
455            0x4000_0066 => Key::Power,
456            0x4000_0067 => Key::NumPadEquals,
457            0x4000_0068 => Key::F13,
458            0x4000_0069 => Key::F14,
459            0x4000_006A => Key::F15,
460            0x4000_006B => Key::F16,
461            0x4000_006C => Key::F17,
462            0x4000_006D => Key::F18,
463            0x4000_006E => Key::F19,
464            0x4000_006F => Key::F20,
465            0x4000_0070 => Key::F21,
466            0x4000_0071 => Key::F22,
467            0x4000_0072 => Key::F23,
468            0x4000_0073 => Key::F24,
469            0x4000_0074 => Key::Execute,
470            0x4000_0075 => Key::Help,
471            0x4000_0076 => Key::Menu,
472            0x4000_0077 => Key::Select,
473            0x4000_0078 => Key::Stop,
474            0x4000_0079 => Key::Again,
475            0x4000_007A => Key::Undo,
476            0x4000_007B => Key::Cut,
477            0x4000_007C => Key::Copy,
478            0x4000_007D => Key::Paste,
479            0x4000_007E => Key::Find,
480            0x4000_007F => Key::Mute,
481            0x4000_0080 => Key::VolumeUp,
482            0x4000_0081 => Key::VolumeDown,
483            0x4000_0085 => Key::NumPadComma,
484            0x4000_0086 => Key::NumPadEqualsAS400,
485            0x4000_0099 => Key::AltErase,
486            0x4000_009A => Key::Sysreq,
487            0x4000_009B => Key::Cancel,
488            0x4000_009C => Key::Clear,
489            0x4000_009D => Key::Prior,
490            0x4000_009E => Key::Return2,
491            0x4000_009F => Key::Separator,
492            0x4000_00A0 => Key::Out,
493            0x4000_00A1 => Key::Oper,
494            0x4000_00A2 => Key::ClearAgain,
495            0x4000_00A3 => Key::CrSel,
496            0x4000_00A4 => Key::ExSel,
497            0x4000_00B0 => Key::NumPad00,
498            0x4000_00B1 => Key::NumPad000,
499            0x4000_00B2 => Key::ThousandsSeparator,
500            0x4000_00B3 => Key::DecimalSeparator,
501            0x4000_00B4 => Key::CurrencyUnit,
502            0x4000_00B5 => Key::CurrencySubUnit,
503            0x4000_00B6 => Key::NumPadLeftParen,
504            0x4000_00B7 => Key::NumPadRightParen,
505            0x4000_00B8 => Key::NumPadLeftBrace,
506            0x4000_00B9 => Key::NumPadRightBrace,
507            0x4000_00BA => Key::NumPadTab,
508            0x4000_00BB => Key::NumPadBackspace,
509            0x4000_00BC => Key::NumPadA,
510            0x4000_00BD => Key::NumPadB,
511            0x4000_00BE => Key::NumPadC,
512            0x4000_00BF => Key::NumPadD,
513            0x4000_00C0 => Key::NumPadE,
514            0x4000_00C1 => Key::NumPadF,
515            0x4000_00C2 => Key::NumPadXor,
516            0x4000_00C3 => Key::NumPadPower,
517            0x4000_00C4 => Key::NumPadPercent,
518            0x4000_00C5 => Key::NumPadLess,
519            0x4000_00C6 => Key::NumPadGreater,
520            0x4000_00C7 => Key::NumPadAmpersand,
521            0x4000_00C8 => Key::NumPadDblAmpersand,
522            0x4000_00C9 => Key::NumPadVerticalBar,
523            0x4000_00CA => Key::NumPadDblVerticalBar,
524            0x4000_00CB => Key::NumPadColon,
525            0x4000_00CC => Key::NumPadHash,
526            0x4000_00CD => Key::NumPadSpace,
527            0x4000_00CE => Key::NumPadAt,
528            0x4000_00CF => Key::NumPadExclam,
529            0x4000_00D0 => Key::NumPadMemStore,
530            0x4000_00D1 => Key::NumPadMemRecall,
531            0x4000_00D2 => Key::NumPadMemClear,
532            0x4000_00D3 => Key::NumPadMemAdd,
533            0x4000_00D4 => Key::NumPadMemSubtract,
534            0x4000_00D5 => Key::NumPadMemMultiply,
535            0x4000_00D6 => Key::NumPadMemDivide,
536            0x4000_00D7 => Key::NumPadPlusMinus,
537            0x4000_00D8 => Key::NumPadClear,
538            0x4000_00D9 => Key::NumPadClearEntry,
539            0x4000_00DA => Key::NumPadBinary,
540            0x4000_00DB => Key::NumPadOctal,
541            0x4000_00DC => Key::NumPadDecimal,
542            0x4000_00DD => Key::NumPadHexadecimal,
543            0x4000_00E0 => Key::LCtrl,
544            0x4000_00E1 => Key::LShift,
545            0x4000_00E2 => Key::LAlt,
546            0x4000_00E3 => Key::LGui,
547            0x4000_00E4 => Key::RCtrl,
548            0x4000_00E5 => Key::RShift,
549            0x4000_00E6 => Key::RAlt,
550            0x4000_00E7 => Key::RGui,
551            0x4000_0101 => Key::Mode,
552            0x4000_0102 => Key::AudioNext,
553            0x4000_0103 => Key::AudioPrev,
554            0x4000_0104 => Key::AudioStop,
555            0x4000_0105 => Key::AudioPlay,
556            0x4000_0106 => Key::AudioMute,
557            0x4000_0107 => Key::MediaSelect,
558            0x4000_0108 => Key::Www,
559            0x4000_0109 => Key::Mail,
560            0x4000_010A => Key::Calculator,
561            0x4000_010B => Key::Computer,
562            0x4000_010C => Key::AcSearch,
563            0x4000_010D => Key::AcHome,
564            0x4000_010E => Key::AcBack,
565            0x4000_010F => Key::AcForward,
566            0x4000_0110 => Key::AcStop,
567            0x4000_0111 => Key::AcRefresh,
568            0x4000_0112 => Key::AcBookmarks,
569            0x4000_0113 => Key::BrightnessDown,
570            0x4000_0114 => Key::BrightnessUp,
571            0x4000_0115 => Key::DisplaySwitch,
572            0x4000_0116 => Key::KbdIllumToggle,
573            0x4000_0117 => Key::KbdIllumDown,
574            0x4000_0118 => Key::KbdIllumUp,
575            0x4000_0119 => Key::Eject,
576            0x4000_011A => Key::Sleep,
577
578            _ => Key::Unknown,
579        }
580    }
581}
582
583impl Key {
584    /// Returns an id of the key
585    #[inline(always)]
586    pub fn code(&self) -> i32 {
587        *self as i32
588    }
589}
590
591impl From<Key> for u32 {
592    #[inline(always)]
593    fn from(key: Key) -> u32 {
594        key as u32
595    }
596}
597
598#[cfg(test)]
599mod tests {
600    #[test]
601    fn keycode() {
602        use super::{Key, Key::*};
603
604        let keys = vec![
605            Unknown,
606            Backspace,
607            Tab,
608            Return,
609            Escape,
610            Space,
611            Exclaim,
612            Quotedbl,
613            Hash,
614            Dollar,
615            Percent,
616            Ampersand,
617            Quote,
618            LeftParen,
619            RightParen,
620            Asterisk,
621            Plus,
622            Comma,
623            Minus,
624            Period,
625            Slash,
626            D0,
627            D1,
628            D2,
629            D3,
630            D4,
631            D5,
632            D6,
633            D7,
634            D8,
635            D9,
636            Colon,
637            Semicolon,
638            Less,
639            Equals,
640            Greater,
641            Question,
642            At,
643            LeftBracket,
644            Backslash,
645            RightBracket,
646            Caret,
647            Underscore,
648            Backquote,
649            A,
650            B,
651            C,
652            D,
653            E,
654            F,
655            G,
656            H,
657            I,
658            J,
659            K,
660            L,
661            M,
662            N,
663            O,
664            P,
665            Q,
666            R,
667            S,
668            T,
669            U,
670            V,
671            W,
672            X,
673            Y,
674            Z,
675            Delete,
676            CapsLock,
677            F1,
678            F2,
679            F3,
680            F4,
681            F5,
682            F6,
683            F7,
684            F8,
685            F9,
686            F10,
687            F11,
688            F12,
689            PrintScreen,
690            ScrollLock,
691            Pause,
692            Insert,
693            Home,
694            PageUp,
695            End,
696            PageDown,
697            Right,
698            Left,
699            Down,
700            Up,
701            NumLockClear,
702            NumPadDivide,
703            NumPadMultiply,
704            NumPadMinus,
705            NumPadPlus,
706            NumPadEnter,
707            NumPad1,
708            NumPad2,
709            NumPad3,
710            NumPad4,
711            NumPad5,
712            NumPad6,
713            NumPad7,
714            NumPad8,
715            NumPad9,
716            NumPad0,
717            NumPadPeriod,
718            Application,
719            Power,
720            NumPadEquals,
721            F13,
722            F14,
723            F15,
724            F16,
725            F17,
726            F18,
727            F19,
728            F20,
729            F21,
730            F22,
731            F23,
732            F24,
733            Execute,
734            Help,
735            Menu,
736            Select,
737            Stop,
738            Again,
739            Undo,
740            Cut,
741            Copy,
742            Paste,
743            Find,
744            Mute,
745            VolumeUp,
746            VolumeDown,
747            NumPadComma,
748            NumPadEqualsAS400,
749            AltErase,
750            Sysreq,
751            Cancel,
752            Clear,
753            Prior,
754            Return2,
755            Separator,
756            Out,
757            Oper,
758            ClearAgain,
759            CrSel,
760            ExSel,
761            NumPad00,
762            NumPad000,
763            ThousandsSeparator,
764            DecimalSeparator,
765            CurrencyUnit,
766            CurrencySubUnit,
767            NumPadLeftParen,
768            NumPadRightParen,
769            NumPadLeftBrace,
770            NumPadRightBrace,
771            NumPadTab,
772            NumPadBackspace,
773            NumPadA,
774            NumPadB,
775            NumPadC,
776            NumPadD,
777            NumPadE,
778            NumPadF,
779            NumPadXor,
780            NumPadPower,
781            NumPadPercent,
782            NumPadLess,
783            NumPadGreater,
784            NumPadAmpersand,
785            NumPadDblAmpersand,
786            NumPadVerticalBar,
787            NumPadDblVerticalBar,
788            NumPadColon,
789            NumPadHash,
790            NumPadSpace,
791            NumPadAt,
792            NumPadExclam,
793            NumPadMemStore,
794            NumPadMemRecall,
795            NumPadMemClear,
796            NumPadMemAdd,
797            NumPadMemSubtract,
798            NumPadMemMultiply,
799            NumPadMemDivide,
800            NumPadPlusMinus,
801            NumPadClear,
802            NumPadClearEntry,
803            NumPadBinary,
804            NumPadOctal,
805            NumPadDecimal,
806            NumPadHexadecimal,
807            LCtrl,
808            LShift,
809            LAlt,
810            LGui,
811            RCtrl,
812            RShift,
813            RAlt,
814            RGui,
815            Mode,
816            AudioNext,
817            AudioPrev,
818            AudioStop,
819            AudioPlay,
820            AudioMute,
821            MediaSelect,
822            Www,
823            Mail,
824            Calculator,
825            Computer,
826            AcSearch,
827            AcHome,
828            AcBack,
829            AcForward,
830            AcStop,
831            AcRefresh,
832            AcBookmarks,
833            BrightnessDown,
834            BrightnessUp,
835            DisplaySwitch,
836            KbdIllumToggle,
837            KbdIllumDown,
838            KbdIllumUp,
839            Eject,
840            Sleep,
841        ];
842        for &key in &keys {
843            let val: u32 = key.into();
844            let key2: Key = val.into();
845            assert_eq!(key, key2);
846        }
847    }
848}