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