Skip to main content

keyflow/
types.rs

1use crate::hotkey::{Hotkey, HotkeyCallback, HotkeyId};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4use std::time::Duration;
5use strum_macros::EnumIter;
6
7/// Key representation
8#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, EnumIter)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub enum Key {
11    A,
12    B,
13    C,
14    D,
15    E,
16    F,
17    G,
18    H,
19    I,
20    J,
21    K,
22    L,
23    M,
24    N,
25    O,
26    P,
27    Q,
28    R,
29    S,
30    T,
31    U,
32    V,
33    W,
34    X,
35    Y,
36    Z,
37
38    Digit1,
39    Digit2,
40    Digit3,
41    Digit4,
42    Digit5,
43    Digit6,
44    Digit7,
45    Digit8,
46    Digit9,
47    Digit0,
48
49    F1,
50    F2,
51    F3,
52    F4,
53    F5,
54    F6,
55    F7,
56    F8,
57    F9,
58    F10,
59    F11,
60    F12,
61    F13,
62    F14,
63    F15,
64    F16,
65    F17,
66    F18,
67    F19,
68    F20,
69    F21,
70    F22,
71    F23,
72    F24,
73
74    ShiftLeft,
75    ShiftRight,
76    ControlLeft,
77    ControlRight,
78    AltLeft,
79    AltRight,
80
81    /// Windows/Super/Command key
82    MetaLeft,
83    MetaRight,
84
85    Escape,
86    Space,
87    Enter,
88    Tab,
89    Backspace,
90    CapsLock,
91    NumLock,
92    ScrollLock,
93
94    Backquote,
95    Minus,
96    Equal,
97    BracketLeft,
98    BracketRight,
99    Backslash,
100    Semicolon,
101    Quote,
102    Comma,
103    Period,
104    Slash,
105
106    /// Extra key on ISO keyboards (between Left Shift and Z)
107    IsoBackslash,
108
109    Insert,
110    Delete,
111    Home,
112    End,
113    PageUp,
114    PageDown,
115
116    ArrowUp,
117    ArrowDown,
118    ArrowLeft,
119    ArrowRight,
120
121    Numpad0,
122    Numpad1,
123    Numpad2,
124    Numpad3,
125    Numpad4,
126    Numpad5,
127    Numpad6,
128    Numpad7,
129    Numpad8,
130    Numpad9,
131    NumpadAdd,
132    NumpadSubtract,
133    NumpadMultiply,
134    NumpadDivide,
135    NumpadDecimal,
136    NumpadEnter,
137    NumpadEqual,
138    NumpadComma,
139
140    PrintScreen,
141    Pause,
142    ContextMenu,
143    Power,
144    Sleep,
145    Wake,
146
147    VolumeMute,
148    VolumeDown,
149    VolumeUp,
150    MediaPlayPause,
151    MediaStop,
152    MediaTrackNext,
153    MediaTrackPrevious,
154
155    BrowserBack,
156    BrowserForward,
157    BrowserRefresh,
158    BrowserStop,
159    BrowserSearch,
160    BrowserFavorites,
161    BrowserHome,
162
163    LaunchMail,
164    LaunchMediaPlayer,
165    LaunchApp1,
166    LaunchApp2,
167
168    Help,
169    Undo,
170    Redo,
171    Cut,
172    Copy,
173    Paste,
174    Find,
175}
176
177/// Mouse button representation
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
179#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
180pub enum Button {
181    Left,
182    Right,
183    Middle,
184    Extra1,
185    Extra2,
186    Extra3,
187    Extra4,
188}
189
190/// Input event action
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
193pub enum Action {
194    Press,
195    Release,
196    Click,
197}
198
199/// Mouse movement type
200#[derive(Debug, Clone, Copy, PartialEq, Eq)]
201#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
202pub enum Movement {
203    Relative { dx: i32, dy: i32 },
204    Absolute { x: i32, y: i32 },
205}
206
207/// Mouse scroll
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
210pub enum Scroll {
211    Vertical(i32),
212    Horizontal(i32),
213}
214
215/// Input event types
216#[derive(Debug, Clone, Copy, PartialEq, Eq)]
217#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
218pub enum InputEvent {
219    KeyEvent {
220        key: Key,
221        action: Action,
222    },
223    ButtonEvent {
224        button: Button,
225        action: Action,
226    },
227    MouseMove(Movement),
228    MouseScroll(Scroll),
229    #[cfg_attr(feature = "serde", serde(with = "serde_duration"))]
230    Delay(Duration),
231}
232
233pub(crate) enum InternalMessage {
234    SimulateEvent(InputEvent),
235    BatchEvents(Vec<InputEvent>),
236    RegisterHotkey {
237        id: HotkeyId,
238        combo: Hotkey,
239        callback: HotkeyCallback,
240    },
241    UnregisterHotkey {
242        id: HotkeyId,
243    },
244}
245
246/// Define the Screen limits
247#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
248pub struct Screen {
249    /// The most left edge of all screens
250    ///
251    /// * 1 screen with 1920x1080 => 0
252    /// * 2 screens with 1920x1080, primary screen left => 0
253    /// * 2 screen with 1920x1080, primary screen right => -1920
254    /// * 2 screen with 1920x1080, primary screen top or bottom => 0
255    pub left: i32,
256    /// The most top edge of all screens
257    ///
258    /// See [`Self::left`]
259    pub top: i32,
260    /// The total width of all screens
261    /// * 1 screen with 1920x1080 => 1920
262    /// * 2 screens with 1920x1080 side by side => 3840
263    /// * 2 screens with 1920x1080 on top of each other => 1920
264    pub width: i32,
265    /// The total height of all screens
266    /// * 1 screen with 1920x1080 => 1080
267    /// * 2 screens with 1920x1080 side by side => 1080
268    /// * 2 screens with 1920x1080 on top of each other => 2160
269    pub height: i32,
270}
271
272impl Screen {
273    pub fn new(left: i32, top: i32, width: i32, height: i32) -> Self {
274        Self {
275            left,
276            top,
277            width,
278            height,
279        }
280    }
281}
282
283impl Default for Screen {
284    /// Returns a default screen with 1920 x 1080.
285    fn default() -> Self {
286        Self::new(0, 0, 1920, 1080)
287    }
288}
289
290#[derive(PartialEq, Debug)]
291pub(crate) enum KeyState {
292    Released,
293    Pressed,
294    Repeated,
295}
296
297impl From<u16> for KeyState {
298    fn from(state: u16) -> Self {
299        match state {
300            0 => KeyState::Released,
301            1 => KeyState::Pressed,
302            2 => KeyState::Repeated,
303            _ => unreachable!(),
304        }
305    }
306}
307
308impl From<i32> for KeyState {
309    fn from(state: i32) -> Self {
310        match state {
311            0 => KeyState::Released,
312            1 => KeyState::Pressed,
313            2 => KeyState::Repeated,
314            _ => unreachable!(),
315        }
316    }
317}
318
319#[cfg(feature = "serde")]
320mod serde_duration {
321    use serde::{Deserialize, Deserializer, Serializer};
322    use std::time::Duration;
323
324    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
325    where
326        S: Serializer,
327    {
328        serializer.serialize_u64(duration.as_millis() as u64)
329    }
330
331    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
332    where
333        D: Deserializer<'de>,
334    {
335        let millis = u64::deserialize(deserializer)?;
336        Ok(Duration::from_millis(millis))
337    }
338}