smart_keymap/key/
keyboard.rs1#![doc = include_str!("doc_de_keyboard.md")]
2
3use serde::Deserialize;
4
5use crate::key;
6
7#[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 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 pub const fn new_with_modifiers(key_code: u8, modifiers: key::KeyboardModifiers) -> Self {
51 Key {
52 key_code,
53 modifiers,
54 }
55 }
56
57 pub const fn from_modifiers(modifiers: key::KeyboardModifiers) -> Self {
59 Key {
60 key_code: 0x00,
61 modifiers,
62 }
63 }
64
65 pub fn key_code(&self) -> u8 {
67 self.key_code
68 }
69
70 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 ) -> (
103 Option<key::PressedKeyResult<Self::PendingKeyState, Self::KeyState>>,
104 key::KeyEvents<Self::Event>,
105 ) {
106 panic!()
107 }
108
109 fn lookup(
110 &self,
111 _path: &[u16],
112 ) -> &dyn key::Key<
113 Context = Self::Context,
114 Event = Self::Event,
115 PendingKeyState = Self::PendingKeyState,
116 KeyState = Self::KeyState,
117 > {
118 self
119 }
120}
121
122#[derive(Debug, Clone, Copy, PartialEq)]
124pub struct KeyState(Key);
125
126impl KeyState {
127 pub fn key_output(&self) -> key::KeyOutput {
129 let KeyState(key) = self;
130 key::KeyOutput::from_key_code_with_modifiers(key.key_code, key.modifiers)
131 }
132}