agui_widgets/state/
keyboard.rs

1use std::collections::HashMap;
2
3pub struct Keyboard {
4    pub keys: HashMap<KeyCode, KeyState>,
5    pub modifiers: Modifiers,
6}
7
8impl Keyboard {
9    pub fn is_pressed(&self, key: &KeyCode) -> bool {
10        self.keys
11            .get(key)
12            .map_or(false, |state| *state == KeyState::Pressed)
13    }
14
15    pub fn is_released(&self, key: &KeyCode) -> bool {
16        self.keys
17            .get(key)
18            .map_or(false, |state| *state == KeyState::Released)
19    }
20}
21
22impl Default for Keyboard {
23    fn default() -> Self {
24        Self {
25            keys: HashMap::default(),
26            modifiers: Modifiers::default(),
27        }
28    }
29}
30
31#[derive(Default)]
32pub struct KeyboardInput(pub char);
33
34/// Describes the input state of a key.
35#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
36pub enum KeyState {
37    Pressed,
38    Released,
39}
40
41#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
42#[repr(u32)]
43pub enum KeyCode {
44    /// The '1' key over the letters.
45    Key1,
46    /// The '2' key over the letters.
47    Key2,
48    /// The '3' key over the letters.
49    Key3,
50    /// The '4' key over the letters.
51    Key4,
52    /// The '5' key over the letters.
53    Key5,
54    /// The '6' key over the letters.
55    Key6,
56    /// The '7' key over the letters.
57    Key7,
58    /// The '8' key over the letters.
59    Key8,
60    /// The '9' key over the letters.
61    Key9,
62    /// The '0' key over the 'O' and 'P' keys.
63    Key0,
64
65    A,
66    B,
67    C,
68    D,
69    E,
70    F,
71    G,
72    H,
73    I,
74    J,
75    K,
76    L,
77    M,
78    N,
79    O,
80    P,
81    Q,
82    R,
83    S,
84    T,
85    U,
86    V,
87    W,
88    X,
89    Y,
90    Z,
91
92    /// The Escape key, next to F1.
93    Escape,
94
95    F1,
96    F2,
97    F3,
98    F4,
99    F5,
100    F6,
101    F7,
102    F8,
103    F9,
104    F10,
105    F11,
106    F12,
107    F13,
108    F14,
109    F15,
110    F16,
111    F17,
112    F18,
113    F19,
114    F20,
115    F21,
116    F22,
117    F23,
118    F24,
119
120    /// Print Screen/SysRq.
121    Snapshot,
122    /// Scroll Lock.
123    Scroll,
124    /// Pause/Break key, next to Scroll lock.
125    Pause,
126
127    /// `Insert`, next to Backspace.
128    Insert,
129    Home,
130    Delete,
131    End,
132    PageDown,
133    PageUp,
134
135    Left,
136    Up,
137    Right,
138    Down,
139
140    /// The Backspace key, right over Enter.
141    // TODO: rename
142    Back,
143    /// The Enter key.
144    Return,
145    /// The space bar.
146    Space,
147
148    /// The "Compose" key on Linux.
149    Compose,
150
151    Caret,
152
153    Numlock,
154    Numpad0,
155    Numpad1,
156    Numpad2,
157    Numpad3,
158    Numpad4,
159    Numpad5,
160    Numpad6,
161    Numpad7,
162    Numpad8,
163    Numpad9,
164    NumpadAdd,
165    NumpadDivide,
166    NumpadDecimal,
167    NumpadComma,
168    NumpadEnter,
169    NumpadEquals,
170    NumpadMultiply,
171    NumpadSubtract,
172
173    AbntC1,
174    AbntC2,
175    Apostrophe,
176    Apps,
177    Asterisk,
178    At,
179    Ax,
180    Backslash,
181    Calculator,
182    Capital,
183    Colon,
184    Comma,
185    Convert,
186    Equals,
187    Grave,
188    Kana,
189    Kanji,
190    LAlt,
191    LBracket,
192    LControl,
193    LShift,
194    LWin,
195    Mail,
196    MediaSelect,
197    MediaStop,
198    Minus,
199    Mute,
200    MyComputer,
201    // also called "Next"
202    NavigateForward,
203    // also called "Prior"
204    NavigateBackward,
205    NextTrack,
206    NoConvert,
207    OEM102,
208    Period,
209    PlayPause,
210    Plus,
211    Power,
212    PrevTrack,
213    RAlt,
214    RBracket,
215    RControl,
216    RShift,
217    RWin,
218    Semicolon,
219    Slash,
220    Sleep,
221    Stop,
222    Sysrq,
223    Tab,
224    Underline,
225    Unlabeled,
226    VolumeDown,
227    VolumeUp,
228    Wake,
229    WebBack,
230    WebFavorites,
231    WebForward,
232    WebHome,
233    WebRefresh,
234    WebSearch,
235    WebStop,
236    Yen,
237    Copy,
238    Paste,
239    Cut,
240}
241
242bitflags::bitflags! {
243    /// Represents the current state of the keyboard modifiers
244    ///
245    /// Each flag represents a modifier and is set if this modifier is active.
246    #[derive(Default)]
247    pub struct Modifiers: u32 {
248        /// The "shift" key.
249        const SHIFT = 0b100;
250        // const LSHIFT = 0b010 << 0;
251        // const RSHIFT = 0b001 << 0;
252        
253        /// The "control" key.
254        const CTRL = 0b100 << 3;
255        // const LCTRL = 0b010 << 3;
256        // const RCTRL = 0b001 << 3;
257
258        /// The "alt" key.
259        const ALT = 0b100 << 6;
260        // const LALT = 0b010 << 6;
261        // const RALT = 0b001 << 6;
262
263        /// This is the "windows" key on PC and "command" key on Mac.
264        const LOGO = 0b100 << 9;
265        // const LLOGO = 0b010 << 9;
266        // const RLOGO = 0b001 << 9;
267    }
268}
269
270impl Modifiers {
271    /// Returns `true` if the shift key is pressed.
272    pub fn shift(&self) -> bool {
273        self.intersects(Self::SHIFT)
274    }
275    /// Returns `true` if the control key is pressed.
276    pub fn ctrl(&self) -> bool {
277        self.intersects(Self::CTRL)
278    }
279    /// Returns `true` if the alt key is pressed.
280    pub fn alt(&self) -> bool {
281        self.intersects(Self::ALT)
282    }
283    /// Returns `true` if the logo key is pressed.
284    pub fn logo(&self) -> bool {
285        self.intersects(Self::LOGO)
286    }
287}