Skip to main content

anybox/
key.rs

1use std::fmt;
2
3/// a keyboard key by its HID usage (page 0x07). the consts cover the usual keys, and `Key(n)` gets you any
4/// other one from 0x04 to 0xE7
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub struct Key(pub u8);
7
8impl Key {
9    pub const A: Key = Key(0x04);
10    pub const B: Key = Key(0x05);
11    pub const C: Key = Key(0x06);
12    pub const D: Key = Key(0x07);
13    pub const E: Key = Key(0x08);
14    pub const F: Key = Key(0x09);
15    pub const G: Key = Key(0x0A);
16    pub const H: Key = Key(0x0B);
17    pub const I: Key = Key(0x0C);
18    pub const J: Key = Key(0x0D);
19    pub const K: Key = Key(0x0E);
20    pub const L: Key = Key(0x0F);
21    pub const M: Key = Key(0x10);
22    pub const N: Key = Key(0x11);
23    pub const O: Key = Key(0x12);
24    pub const P: Key = Key(0x13);
25    pub const Q: Key = Key(0x14);
26    pub const R: Key = Key(0x15);
27    pub const S: Key = Key(0x16);
28    pub const T: Key = Key(0x17);
29    pub const U: Key = Key(0x18);
30    pub const V: Key = Key(0x19);
31    pub const W: Key = Key(0x1A);
32    pub const X: Key = Key(0x1B);
33    pub const Y: Key = Key(0x1C);
34    pub const Z: Key = Key(0x1D);
35    pub const NUM1: Key = Key(0x1E);
36    pub const NUM2: Key = Key(0x1F);
37    pub const NUM3: Key = Key(0x20);
38    pub const NUM4: Key = Key(0x21);
39    pub const NUM5: Key = Key(0x22);
40    pub const NUM6: Key = Key(0x23);
41    pub const NUM7: Key = Key(0x24);
42    pub const NUM8: Key = Key(0x25);
43    pub const NUM9: Key = Key(0x26);
44    pub const NUM0: Key = Key(0x27);
45    pub const ENTER: Key = Key(0x28);
46    pub const ESCAPE: Key = Key(0x29);
47    pub const BACKSPACE: Key = Key(0x2A);
48    pub const TAB: Key = Key(0x2B);
49    pub const SPACE: Key = Key(0x2C);
50    pub const MINUS: Key = Key(0x2D);
51    pub const EQUAL: Key = Key(0x2E);
52    pub const LEFT_BRACKET: Key = Key(0x2F);
53    pub const RIGHT_BRACKET: Key = Key(0x30);
54    pub const BACKSLASH: Key = Key(0x31);
55    pub const SEMICOLON: Key = Key(0x33);
56    pub const APOSTROPHE: Key = Key(0x34);
57    pub const GRAVE: Key = Key(0x35);
58    pub const COMMA: Key = Key(0x36);
59    pub const PERIOD: Key = Key(0x37);
60    pub const SLASH: Key = Key(0x38);
61    pub const CAPS_LOCK: Key = Key(0x39);
62    pub const F1: Key = Key(0x3A);
63    pub const F2: Key = Key(0x3B);
64    pub const F3: Key = Key(0x3C);
65    pub const F4: Key = Key(0x3D);
66    pub const F5: Key = Key(0x3E);
67    pub const F6: Key = Key(0x3F);
68    pub const F7: Key = Key(0x40);
69    pub const F8: Key = Key(0x41);
70    pub const F9: Key = Key(0x42);
71    pub const F10: Key = Key(0x43);
72    pub const F11: Key = Key(0x44);
73    pub const F12: Key = Key(0x45);
74    pub const PRINT_SCREEN: Key = Key(0x46);
75    pub const SCROLL_LOCK: Key = Key(0x47);
76    pub const PAUSE: Key = Key(0x48);
77    pub const INSERT: Key = Key(0x49);
78    pub const HOME: Key = Key(0x4A);
79    pub const PAGE_UP: Key = Key(0x4B);
80    pub const DELETE: Key = Key(0x4C);
81    pub const END: Key = Key(0x4D);
82    pub const PAGE_DOWN: Key = Key(0x4E);
83    pub const RIGHT: Key = Key(0x4F);
84    pub const LEFT: Key = Key(0x50);
85    pub const DOWN: Key = Key(0x51);
86    pub const UP: Key = Key(0x52);
87    pub const NUM_LOCK: Key = Key(0x53);
88    pub const KEYPAD_DIVIDE: Key = Key(0x54);
89    pub const KEYPAD_MULTIPLY: Key = Key(0x55);
90    pub const KEYPAD_MINUS: Key = Key(0x56);
91    pub const KEYPAD_PLUS: Key = Key(0x57);
92    pub const KEYPAD_ENTER: Key = Key(0x58);
93    pub const KEYPAD1: Key = Key(0x59);
94    pub const KEYPAD2: Key = Key(0x5A);
95    pub const KEYPAD3: Key = Key(0x5B);
96    pub const KEYPAD4: Key = Key(0x5C);
97    pub const KEYPAD5: Key = Key(0x5D);
98    pub const KEYPAD6: Key = Key(0x5E);
99    pub const KEYPAD7: Key = Key(0x5F);
100    pub const KEYPAD8: Key = Key(0x60);
101    pub const KEYPAD9: Key = Key(0x61);
102    pub const KEYPAD0: Key = Key(0x62);
103    pub const KEYPAD_PERIOD: Key = Key(0x63);
104    pub const MENU: Key = Key(0x65);
105    pub const LEFT_CTRL: Key = Key(0xE0);
106    pub const LEFT_SHIFT: Key = Key(0xE1);
107    pub const LEFT_ALT: Key = Key(0xE2);
108    pub const LEFT_GUI: Key = Key(0xE3);
109    pub const RIGHT_CTRL: Key = Key(0xE4);
110    pub const RIGHT_SHIFT: Key = Key(0xE5);
111    pub const RIGHT_ALT: Key = Key(0xE6);
112    pub const RIGHT_GUI: Key = Key(0xE7);
113
114    /// ctrl, shift, alt and gui on either side. a keyboard report keeps these in a byte of their own
115    pub const fn is_modifier(self) -> bool {
116        matches!(self.0, 0xE0..=0xE7)
117    }
118
119    /// 0x00 to 0x03 are HID error codes, and nothing from 0xE8 up is defined on the keyboard page
120    pub(crate) const fn is_valid(self) -> bool {
121        matches!(self.0, 0x04..=0xE7)
122    }
123}
124
125impl fmt::Display for Key {
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        write!(f, "key 0x{:02X}", self.0)
128    }
129}
130
131/// a media key by its HID consumer usage (page 0x0C)
132#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
133pub struct MediaKey(pub u16);
134
135impl MediaKey {
136    pub const PLAY: MediaKey = MediaKey(0xB0);
137    pub const PAUSE: MediaKey = MediaKey(0xB1);
138    pub const NEXT_TRACK: MediaKey = MediaKey(0xB5);
139    pub const PREVIOUS_TRACK: MediaKey = MediaKey(0xB6);
140    pub const STOP: MediaKey = MediaKey(0xB7);
141    pub const PLAY_PAUSE: MediaKey = MediaKey(0xCD);
142    pub const MUTE: MediaKey = MediaKey(0xE2);
143    pub const VOLUME_UP: MediaKey = MediaKey(0xE9);
144    pub const VOLUME_DOWN: MediaKey = MediaKey(0xEA);
145}
146
147impl fmt::Display for MediaKey {
148    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149        write!(f, "media key 0x{:04X}", self.0)
150    }
151}