1use std::fmt;
7
8pub use cranpose_foundation::Modifiers;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum KeyEventType {
13 KeyDown,
15 KeyUp,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub enum KeyCode {
25 A,
27 B,
29 C,
31 D,
33 E,
35 F,
37 G,
39 H,
41 I,
43 J,
45 K,
47 L,
49 M,
51 N,
53 O,
55 P,
57 Q,
59 R,
61 S,
63 T,
65 U,
67 V,
69 W,
71 X,
73 Y,
75 Z,
77
78 Digit0,
80 Digit1,
82 Digit2,
84 Digit3,
86 Digit4,
88 Digit5,
90 Digit6,
92 Digit7,
94 Digit8,
96 Digit9,
98
99 F1,
101 F2,
103 F3,
105 F4,
107 F5,
109 F6,
111 F7,
113 F8,
115 F9,
117 F10,
119 F11,
121 F12,
123
124 ArrowUp,
126 ArrowDown,
128 ArrowLeft,
130 ArrowRight,
132 Home,
134 End,
136 PageUp,
138 PageDown,
140
141 Backspace,
143 Delete,
145 Enter,
147 Tab,
149 Space,
151 Escape,
153
154 ShiftLeft,
156 ShiftRight,
158 ControlLeft,
160 ControlRight,
162 AltLeft,
164 AltRight,
166 MetaLeft,
168 MetaRight,
170
171 Minus,
173 Equal,
175 BracketLeft,
177 BracketRight,
179 Backslash,
181 Semicolon,
183 Quote,
185 Comma,
187 Period,
189 Slash,
191 Backquote,
193
194 Unknown,
196}
197
198const DOM_KEY_CODES: [(&str, KeyCode); 82] = [
199 ("KeyA", KeyCode::A),
200 ("KeyB", KeyCode::B),
201 ("KeyC", KeyCode::C),
202 ("KeyD", KeyCode::D),
203 ("KeyE", KeyCode::E),
204 ("KeyF", KeyCode::F),
205 ("KeyG", KeyCode::G),
206 ("KeyH", KeyCode::H),
207 ("KeyI", KeyCode::I),
208 ("KeyJ", KeyCode::J),
209 ("KeyK", KeyCode::K),
210 ("KeyL", KeyCode::L),
211 ("KeyM", KeyCode::M),
212 ("KeyN", KeyCode::N),
213 ("KeyO", KeyCode::O),
214 ("KeyP", KeyCode::P),
215 ("KeyQ", KeyCode::Q),
216 ("KeyR", KeyCode::R),
217 ("KeyS", KeyCode::S),
218 ("KeyT", KeyCode::T),
219 ("KeyU", KeyCode::U),
220 ("KeyV", KeyCode::V),
221 ("KeyW", KeyCode::W),
222 ("KeyX", KeyCode::X),
223 ("KeyY", KeyCode::Y),
224 ("KeyZ", KeyCode::Z),
225 ("Digit0", KeyCode::Digit0),
226 ("Digit1", KeyCode::Digit1),
227 ("Digit2", KeyCode::Digit2),
228 ("Digit3", KeyCode::Digit3),
229 ("Digit4", KeyCode::Digit4),
230 ("Digit5", KeyCode::Digit5),
231 ("Digit6", KeyCode::Digit6),
232 ("Digit7", KeyCode::Digit7),
233 ("Digit8", KeyCode::Digit8),
234 ("Digit9", KeyCode::Digit9),
235 ("F1", KeyCode::F1),
236 ("F2", KeyCode::F2),
237 ("F3", KeyCode::F3),
238 ("F4", KeyCode::F4),
239 ("F5", KeyCode::F5),
240 ("F6", KeyCode::F6),
241 ("F7", KeyCode::F7),
242 ("F8", KeyCode::F8),
243 ("F9", KeyCode::F9),
244 ("F10", KeyCode::F10),
245 ("F11", KeyCode::F11),
246 ("F12", KeyCode::F12),
247 ("ArrowUp", KeyCode::ArrowUp),
248 ("ArrowDown", KeyCode::ArrowDown),
249 ("ArrowLeft", KeyCode::ArrowLeft),
250 ("ArrowRight", KeyCode::ArrowRight),
251 ("Home", KeyCode::Home),
252 ("End", KeyCode::End),
253 ("PageUp", KeyCode::PageUp),
254 ("PageDown", KeyCode::PageDown),
255 ("Backspace", KeyCode::Backspace),
256 ("Delete", KeyCode::Delete),
257 ("Enter", KeyCode::Enter),
258 ("NumpadEnter", KeyCode::Enter),
259 ("Tab", KeyCode::Tab),
260 ("Space", KeyCode::Space),
261 ("Escape", KeyCode::Escape),
262 ("ShiftLeft", KeyCode::ShiftLeft),
263 ("ShiftRight", KeyCode::ShiftRight),
264 ("ControlLeft", KeyCode::ControlLeft),
265 ("ControlRight", KeyCode::ControlRight),
266 ("AltLeft", KeyCode::AltLeft),
267 ("AltRight", KeyCode::AltRight),
268 ("MetaLeft", KeyCode::MetaLeft),
269 ("MetaRight", KeyCode::MetaRight),
270 ("Minus", KeyCode::Minus),
271 ("Equal", KeyCode::Equal),
272 ("BracketLeft", KeyCode::BracketLeft),
273 ("BracketRight", KeyCode::BracketRight),
274 ("Backslash", KeyCode::Backslash),
275 ("Semicolon", KeyCode::Semicolon),
276 ("Quote", KeyCode::Quote),
277 ("Comma", KeyCode::Comma),
278 ("Period", KeyCode::Period),
279 ("Slash", KeyCode::Slash),
280 ("Backquote", KeyCode::Backquote),
281];
282
283impl KeyCode {
284 pub fn from_dom_code(code: &str) -> Self {
288 DOM_KEY_CODES
289 .iter()
290 .find(|(name, _)| *name == code)
291 .map_or(Self::Unknown, |(_, key)| *key)
292 }
293}
294
295#[derive(Debug, Clone, PartialEq, Eq)]
300pub struct KeyEvent {
301 pub key_code: KeyCode,
303 pub text: String,
306 pub modifiers: Modifiers,
308 pub event_type: KeyEventType,
310}
311
312impl KeyEvent {
313 pub fn new(
315 key_code: KeyCode,
316 text: impl Into<String>,
317 modifiers: Modifiers,
318 event_type: KeyEventType,
319 ) -> Self {
320 Self {
321 key_code,
322 text: text.into(),
323 modifiers,
324 event_type,
325 }
326 }
327
328 pub fn key_down(key_code: KeyCode, text: impl Into<String>) -> Self {
330 Self::new(key_code, text, Modifiers::NONE, KeyEventType::KeyDown)
331 }
332
333 pub fn key_down_with_modifiers(
335 key_code: KeyCode,
336 text: impl Into<String>,
337 modifiers: Modifiers,
338 ) -> Self {
339 Self::new(key_code, text, modifiers, KeyEventType::KeyDown)
340 }
341
342 pub fn is_key_down(&self) -> bool {
344 self.event_type == KeyEventType::KeyDown
345 }
346
347 pub fn has_text(&self) -> bool {
349 !self.text.is_empty()
350 }
351}
352
353impl fmt::Display for KeyEvent {
354 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355 write!(
356 f,
357 "KeyEvent({:?}, text=\"{}\", {:?})",
358 self.key_code, self.text, self.event_type
359 )
360 }
361}
362
363#[cfg(test)]
364#[path = "tests/key_event_tests.rs"]
365mod tests;