Skip to main content

strop_core/
frontend_input.rs

1//! Physical/logical input facts before an editor or terminal chooses semantics.
2//! No toolkit types, grammar normalization, native work or implicit key expansion.
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6pub enum KeyCode {
7    Char(char),
8    Function(u8),
9    Escape,
10    Enter,
11    Backspace,
12    Up,
13    Down,
14    Left,
15    Right,
16    Home,
17    End,
18    PageUp,
19    PageDown,
20    Tab,
21    BackTab,
22    Delete,
23    Insert,
24    Null,
25    CapsLock,
26    ScrollLock,
27    NumLock,
28    PrintScreen,
29    Pause,
30    Menu,
31    KeypadBegin,
32    Media(MediaKey),
33    Modifier(ModifierKey),
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
37pub enum MediaKey {
38    Play,
39    Pause,
40    PlayPause,
41    Reverse,
42    Stop,
43    FastForward,
44    Rewind,
45    TrackNext,
46    TrackPrevious,
47    Record,
48    LowerVolume,
49    RaiseVolume,
50    MuteVolume,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
54pub enum ModifierKey {
55    LeftShift,
56    LeftControl,
57    LeftAlt,
58    LeftSuper,
59    LeftHyper,
60    LeftMeta,
61    RightShift,
62    RightControl,
63    RightAlt,
64    RightSuper,
65    RightHyper,
66    RightMeta,
67    IsoLevel3Shift,
68    IsoLevel5Shift,
69}
70
71#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
72pub struct Modifiers {
73    pub shift: bool,
74    pub control: bool,
75    pub alt: bool,
76    pub super_key: bool,
77    pub hyper: bool,
78    pub meta: bool,
79}
80
81#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
82pub enum KeyKind {
83    #[default]
84    Press,
85    Repeat,
86    Release,
87}
88
89#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
90pub struct KeyState {
91    pub keypad: bool,
92    pub caps_lock: bool,
93    pub num_lock: bool,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
97pub struct KeyEvent {
98    pub code: KeyCode,
99    pub modifiers: Modifiers,
100    pub kind: KeyKind,
101    pub state: KeyState,
102}
103
104impl KeyEvent {
105    pub const fn press(code: KeyCode) -> Self {
106        Self {
107            code,
108            modifiers: Modifiers {
109                shift: false,
110                control: false,
111                alt: false,
112                super_key: false,
113                hyper: false,
114                meta: false,
115            },
116            kind: KeyKind::Press,
117            state: KeyState {
118                keypad: false,
119                caps_lock: false,
120                num_lock: false,
121            },
122        }
123    }
124}
125
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127pub enum Input {
128    Key(KeyEvent),
129    /// Committed text, not a physical-key reconstruction or IME preedit.
130    Text(String),
131    Paste(String),
132}