devela/ui/event/key/key.rs
1// devela::ui::event::key::key
2//
3//! Defines [`Key`].
4//
5
6use crate::{KeyAlpha, KeyMedia, KeyMod, KeyPad};
7
8/// Keyboard codes, used in [`EventKey`][crate::EventKey].
9///
10#[doc = "See also [`KeyFfi`][super::KeyFfi]."]
11//
12// - https://docs.rs/crossterm/latest/crossterm/event/enum.Code.html
13// - https://docs.rs/notcurses/latest/notcurses/struct.Code.html
14// - https://github.com/dankamongmen/notcurses/blob/b0f19f9f296bed44ee2ca69b0cbff1b2b29902f0/src/lib/in.c#L180
15// - https://man.archlinux.org/man/terminfo.5.en
16// - https://docs.rs/sdl2/latest/sdl2/keyboard/enum.Keycode.html (235)
17// - https://docs.rs/sdl2/latest/sdl2/keyboard/enum.Scancode.html (241)
18#[non_exhaustive]
19#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
20pub enum Key {
21 /// Unknown key code (default).
22 // mapped to:
23 // - crossterm: Null,
24 #[default]
25 Unknown,
26
27 /* common control keys */
28 /// Backspace key.
29 Backspace,
30 /// Enter key.
31 Enter,
32 /// Tab key.
33 Tab,
34 // NOTE crossterm returns this from Shift + Tab, but we deconstruct it.
35 // BackTab,
36 /// Escape key.
37 Escape,
38
39 ///
40 // NOTE: from crossterm this is received as ' ' character, but we convert it.
41 Space,
42
43 /* navigation keys */
44 /// Left arrow key.
45 Left,
46 /// Right arrow key.
47 Right,
48 /// Up arrow key.
49 Up,
50 /// Down arrow key.
51 Down,
52
53 /// Home key.
54 Home,
55 /// End key.
56 End,
57 /// Page up key.
58 PageUp,
59 /// Page down key.
60 PageDown,
61
62 /* editing keys */
63 /// Delete key.
64 Delete,
65 /// Insert key.
66 Insert,
67
68 /// A function key.
69 ///
70 /// - Normal: F1-F12
71 /// - +Shift: F13-F24
72 /// - +Control: F25-F36
73 /// - +Shift+Control: F37-F48
74 F(u8),
75
76 /* lock keys */
77 /// Caps Lock key.
78 CapsLock,
79 /// Scroll Lock key.
80 ScrollLock,
81 /// Num Lock key.
82 NumLock,
83
84 /* Special system keys */
85 /// Print Screen key.
86 PrintScreen,
87 /// Pause key.
88 Pause,
89 /// Menu key.
90 Menu,
91
92 /// An alphanumeric key (A-Z, 0-9).
93 Alpha(KeyAlpha),
94
95 /// A Unicode character (text input, international layouts, fallback).
96 Char(char),
97
98 /// A keypad key.
99 Pad(KeyPad),
100 // - https://docs.rs/crossterm/latest/crossterm/event/enum.KeyCode.html#variant.KeypadBegin
101 // - WAIT; https://github.com/crossterm-rs/crossterm/issues/860
102 // KeypadBegin,
103 /// A multimedia key.
104 Media(KeyMedia),
105 /// A modifer key.
106 Mod(KeyMod),
107}
108#[allow(non_upper_case_globals)]
109impl Key {
110 /// Alias of [`Escape`][Key::Escape].
111 pub const Esc: Key = Key::Escape;
112 /// Alias of [`Insert`][Key::Insert].
113 pub const Ins: Key = Key::Insert;
114 /// Alias of [`Delete`][Key::Delete].
115 pub const Del: Key = Key::Delete;
116}
117
118crate::sf! {
119 impl From<KeyAlpha> for Key { fn from(code: KeyAlpha) -> Key { Key::Alpha(code) } }
120 impl From<KeyMedia> for Key { fn from(code: KeyMedia) -> Key { Key::Media(code) } }
121 impl From<KeyMod> for Key { fn from(code: KeyMod) -> Key { Key::Mod(code) } }
122 impl From<KeyPad> for Key { fn from(code: KeyPad) -> Key { Key::Pad(code) } }
123}