Skip to main content

requestty_ui/events/
keys.rs

1bitflags::bitflags! {
2    /// Represents key modifiers (shift, control, alt).
3    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4    pub struct KeyModifiers: u8 {
5        #[allow(missing_docs)]
6        const SHIFT = 0b0000_0001;
7        #[allow(missing_docs)]
8        const CONTROL = 0b0000_0010;
9        #[allow(missing_docs)]
10        const ALT = 0b0000_0100;
11    }
12}
13
14/// Represents a key event.
15#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
16pub struct KeyEvent {
17    /// The key itself.
18    pub code: KeyCode,
19    /// Additional key modifiers.
20    pub modifiers: KeyModifiers,
21}
22
23impl KeyEvent {
24    /// Creates a new `KeyEvent`
25    pub fn new(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
26        KeyEvent { code, modifiers }
27    }
28}
29
30impl From<KeyCode> for KeyEvent {
31    fn from(code: KeyCode) -> Self {
32        KeyEvent {
33            code,
34            modifiers: KeyModifiers::empty(),
35        }
36    }
37}
38
39/// Represents a key.
40#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
41pub enum KeyCode {
42    /// Backspace key.
43    Backspace,
44    /// Enter key.
45    Enter,
46    /// Left arrow key.
47    Left,
48    /// Right arrow key.
49    Right,
50    /// Up arrow key.
51    Up,
52    /// Down arrow key.
53    Down,
54    /// Home key.
55    Home,
56    /// End key.
57    End,
58    /// Page up key.
59    PageUp,
60    /// Page dow key.
61    PageDown,
62    /// Tab key.
63    Tab,
64    /// Shift + Tab key.
65    BackTab,
66    /// Delete key.
67    Delete,
68    /// Insert key.
69    Insert,
70    /// A function key.
71    ///
72    /// `KeyEvent::F(1)` represents F1 key, etc.
73    F(u8),
74    /// A character.
75    ///
76    /// `KeyEvent::Char('c')` represents `c` character, etc.
77    Char(char),
78    /// Null.
79    Null,
80    /// Escape key.
81    Esc,
82}