smart_keymap/key/
custom.rs

1use serde::Deserialize;
2
3use crate::key;
4
5/// A key for HID Keyboard usage codes.
6#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
7pub struct Key {
8    custom: u8,
9}
10
11impl Key {
12    /// Constructs a key with the given custom key indices.
13    pub const fn new(i: u8) -> Self {
14        Key { custom: i }
15    }
16
17    /// Constructs a pressed key state
18    pub fn new_pressed_key(&self) -> KeyState {
19        KeyState(*self)
20    }
21}
22
23impl key::Key for Key {
24    type Context = crate::init::Context;
25    type Event = crate::init::Event;
26    type PendingKeyState = crate::init::PendingKeyState;
27    type KeyState = crate::init::KeyState;
28
29    fn new_pressed_key(
30        &self,
31        _context: &Self::Context,
32        _key_path: key::KeyPath,
33    ) -> (
34        key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>,
35        key::KeyEvents<Self::Event>,
36    ) {
37        let k_ks = self.new_pressed_key();
38        let pks = key::PressedKeyResult::Resolved(k_ks.into());
39        let pke = key::KeyEvents::no_events();
40        (pks, pke)
41    }
42
43    fn handle_event(
44        &self,
45        _pending_state: &mut Self::PendingKeyState,
46        _context: &Self::Context,
47        _key_path: key::KeyPath,
48        _event: key::Event<Self::Event>,
49    ) -> (
50        Option<key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>>,
51        key::KeyEvents<Self::Event>,
52    ) {
53        panic!()
54    }
55
56    fn lookup(
57        &self,
58        _path: &[u16],
59    ) -> &dyn key::Key<
60        Context = Self::Context,
61        Event = Self::Event,
62        PendingKeyState = Self::PendingKeyState,
63        KeyState = Self::KeyState,
64    > {
65        self
66    }
67}
68
69/// [crate::key::KeyState] for [Key]. (crate::key::keyboard pressed keys don't have state).
70#[derive(Debug, Clone, Copy, PartialEq)]
71pub struct KeyState(Key);
72
73impl KeyState {
74    /// Custom key always has a key_output.
75    pub fn key_output(&self) -> key::KeyOutput {
76        let KeyState(key) = self;
77        key::KeyOutput::from_custom_code(key.custom)
78    }
79}