Skip to main content

smart_keymap_core/
input.rs

1use serde::{Deserialize, Serialize};
2
3use crate::key;
4
5/// Input events for [crate::keymap::Keymap].
6#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
7pub enum Event {
8    /// A physical key press for a given `keymap_index`.
9    Press {
10        /// The index of the key in the keymap.
11        keymap_index: u16,
12    },
13    /// A physical key release for a given `keymap_index`.
14    Release {
15        /// The index of the key in the keymap.
16        keymap_index: u16,
17    },
18    /// A virtual key press for a given `key_code`.
19    VirtualKeyPress {
20        /// The virtual key code.
21        key_output: key::KeyOutput,
22    },
23    /// A virtual key release for a given `key_code`.
24    VirtualKeyRelease {
25        /// The virtual key code.
26        key_output: key::KeyOutput,
27    },
28}
29
30impl Event {
31    /// Physical press of the key at `keymap_index`.
32    pub const fn press(keymap_index: u16) -> Self {
33        Self::Press { keymap_index }
34    }
35
36    /// Physical release of the key at `keymap_index`.
37    pub const fn release(keymap_index: u16) -> Self {
38        Self::Release { keymap_index }
39    }
40}
41
42/// A struct for associating a key ref with a [crate::key::KeyState].
43#[derive(Debug, Clone, Copy, PartialEq)]
44pub struct PressedKey<R, S> {
45    /// The index of the pressed key in some keymap.
46    pub keymap_index: u16,
47    /// The Ref to the key data of the key state.
48    pub key_ref: R,
49    /// The pressed key state.
50    pub key_state: S,
51}
52
53/// State resulting from [Event].
54#[derive(Debug, Clone, Copy)]
55pub enum PressedInput<KR, KS> {
56    /// Physically pressed key.
57    Key(PressedKey<KR, KS>),
58    /// Virtually pressed key, and its keycode.
59    Virtual(key::KeyOutput),
60}
61
62impl<KR, KS> PressedInput<KR, KS> {
63    /// Constructor for a [PressedInput::Key].
64    pub fn pressed_key(keymap_index: u16, key_ref: KR, key_state: KS) -> Self {
65        Self::Key(PressedKey {
66            keymap_index,
67            key_ref,
68            key_state,
69        })
70    }
71}