smart_keymap/key/
keyboard.rs

1#![doc = include_str!("doc_de_keyboard.md")]
2
3use serde::Deserialize;
4
5use crate::key;
6
7/// A key for HID Keyboard usage codes.
8#[derive(Deserialize, Clone, Copy, PartialEq)]
9pub struct Key {
10    #[serde(default)]
11    key_code: u8,
12    #[serde(default)]
13    modifiers: key::KeyboardModifiers,
14}
15
16impl core::fmt::Debug for Key {
17    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        match (
19            self.key_code != 0x00,
20            self.modifiers != key::KeyboardModifiers::new(),
21        ) {
22            (true, true) => f
23                .debug_struct("Key")
24                .field("key_code", &self.key_code)
25                .field("modifiers", &self.modifiers)
26                .finish(),
27            (false, true) => f
28                .debug_struct("Key")
29                .field("modifiers", &self.modifiers)
30                .finish(),
31            _ => f
32                .debug_struct("Key")
33                .field("key_code", &self.key_code)
34                .finish(),
35        }
36    }
37}
38
39impl Key {
40    /// Constructs a key with the given key_code.
41    pub const fn new(key_code: u8) -> Self {
42        let modifiers = key::KeyboardModifiers::new();
43        Key {
44            key_code,
45            modifiers,
46        }
47    }
48
49    /// Constructs a key with the given key_code and modifiers.
50    pub const fn new_with_modifiers(key_code: u8, modifiers: key::KeyboardModifiers) -> Self {
51        Key {
52            key_code,
53            modifiers,
54        }
55    }
56
57    /// Constructs a key with the given modifiers.
58    pub const fn from_modifiers(modifiers: key::KeyboardModifiers) -> Self {
59        Key {
60            key_code: 0x00,
61            modifiers,
62        }
63    }
64
65    /// Gets the key code from [Key].
66    pub fn key_code(&self) -> u8 {
67        self.key_code
68    }
69
70    /// Constructs a pressed key state
71    pub fn new_pressed_key(&self) -> KeyState {
72        KeyState(*self)
73    }
74}
75
76impl key::Key for Key {
77    type Context = crate::init::Context;
78    type Event = crate::init::Event;
79    type PendingKeyState = crate::init::PendingKeyState;
80    type KeyState = crate::init::KeyState;
81
82    fn new_pressed_key(
83        &self,
84        _context: &Self::Context,
85        _key_path: key::KeyPath,
86    ) -> (
87        key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>,
88        key::KeyEvents<Self::Event>,
89    ) {
90        let k_ks = self.new_pressed_key();
91        let pks = key::PressedKeyResult::Resolved(k_ks.into());
92        let pke = key::KeyEvents::no_events();
93        (pks, pke)
94    }
95
96    fn handle_event(
97        &self,
98        _pending_state: &mut Self::PendingKeyState,
99        _context: &Self::Context,
100        _key_path: key::KeyPath,
101        _event: key::Event<Self::Event>,
102    ) -> (Option<key::NewPressedKey>, key::KeyEvents<Self::Event>) {
103        panic!()
104    }
105
106    fn lookup(
107        &self,
108        _path: &[u16],
109    ) -> &dyn key::Key<
110        Context = Self::Context,
111        Event = Self::Event,
112        PendingKeyState = Self::PendingKeyState,
113        KeyState = Self::KeyState,
114    > {
115        self
116    }
117}
118
119/// [crate::key::KeyState] for [Key]. (crate::key::keyboard pressed keys don't have state).
120#[derive(Debug, Clone, Copy, PartialEq)]
121pub struct KeyState(Key);
122
123impl KeyState {
124    /// Keyboard key always has a key_output.
125    pub fn key_output(&self) -> key::KeyOutput {
126        let KeyState(key) = self;
127        key::KeyOutput::from_key_code_with_modifiers(key.key_code, key.modifiers)
128    }
129}