smart_keymap/
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
30/// A struct for associating a [crate::key::Key] with a [crate::key::KeyState].
31#[derive(Debug, Clone, Copy, PartialEq)]
32pub struct PressedKey<S> {
33    /// The index of the pressed key in some keymap.
34    pub keymap_index: u16,
35    /// The pressed key state.
36    pub key_state: S,
37}
38
39impl<Ctx, Ev, S: crate::key::KeyState<Context = Ctx, Event = Ev>> PressedKey<S> {
40    /// Convenience passthrough to key_state handle_event.
41    pub fn handle_event(
42        &mut self,
43        context: &Ctx,
44        event: crate::key::Event<Ev>,
45    ) -> crate::key::KeyEvents<Ev> {
46        self.key_state
47            .handle_event(context, self.keymap_index, event)
48    }
49
50    /// Convenience passthrough to key_state key_output.
51    pub fn key_output(&self) -> Option<key::KeyOutput> {
52        self.key_state.key_output()
53    }
54}
55
56/// State resulting from [Event].
57#[derive(Debug, Clone, Copy)]
58pub enum PressedInput<PK> {
59    /// Physically pressed key.
60    Key(PressedKey<PK>),
61    /// Virtually pressed key, and its keycode.
62    Virtual(key::KeyOutput),
63}
64
65impl<PK> PressedInput<PK> {
66    /// Constructor for a [PressedInput::Key].
67    pub fn pressed_key(key_state: PK, keymap_index: u16) -> Self {
68        Self::Key(PressedKey {
69            keymap_index,
70            key_state,
71        })
72    }
73}