1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! Keyboard input event types for Cranpose.
//!
//! This module provides platform-independent keyboard event types
//! that are used to route keyboard input to focused components.
use std::fmt;
pub use cranpose_foundation::Modifiers;
/// Type of keyboard event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyEventType {
/// Key was pressed down.
KeyDown,
/// Key was released.
KeyUp,
}
/// Physical key codes for keyboard input.
///
/// These represent physical keys on the keyboard, independent of
/// the character they produce (which depends on keyboard layout).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
/// The A key.
A,
/// The B key.
B,
/// The C key.
C,
/// The D key.
D,
/// The E key.
E,
/// The F key.
F,
/// The G key.
G,
/// The H key.
H,
/// The I key.
I,
/// The J key.
J,
/// The K key.
K,
/// The L key.
L,
/// The M key.
M,
/// The N key.
N,
/// The O key.
O,
/// The P key.
P,
/// The Q key.
Q,
/// The R key.
R,
/// The S key.
S,
/// The T key.
T,
/// The U key.
U,
/// The V key.
V,
/// The W key.
W,
/// The X key.
X,
/// The Y key.
Y,
/// The Z key.
Z,
/// The 0 key on the main number row.
Digit0,
/// The 1 key on the main number row.
Digit1,
/// The 2 key on the main number row.
Digit2,
/// The 3 key on the main number row.
Digit3,
/// The 4 key on the main number row.
Digit4,
/// The 5 key on the main number row.
Digit5,
/// The 6 key on the main number row.
Digit6,
/// The 7 key on the main number row.
Digit7,
/// The 8 key on the main number row.
Digit8,
/// The 9 key on the main number row.
Digit9,
/// The F1 function key.
F1,
/// The F2 function key.
F2,
/// The F3 function key.
F3,
/// The F4 function key.
F4,
/// The F5 function key.
F5,
/// The F6 function key.
F6,
/// The F7 function key.
F7,
/// The F8 function key.
F8,
/// The F9 function key.
F9,
/// The F10 function key.
F10,
/// The F11 function key.
F11,
/// The F12 function key.
F12,
/// The up arrow key.
ArrowUp,
/// The down arrow key.
ArrowDown,
/// The left arrow key.
ArrowLeft,
/// The right arrow key.
ArrowRight,
/// The Home key.
Home,
/// The End key.
End,
/// The Page Up key.
PageUp,
/// The Page Down key.
PageDown,
/// The Backspace key, which deletes backward.
Backspace,
/// The Delete key, which deletes forward.
Delete,
/// The main Enter or Return key.
Enter,
/// The Tab key.
Tab,
/// The space bar.
Space,
/// The Escape key.
Escape,
/// The left Shift key.
ShiftLeft,
/// The right Shift key.
ShiftRight,
/// The left Control key.
ControlLeft,
/// The right Control key.
ControlRight,
/// The left Alt key, called Option on macOS.
AltLeft,
/// The right Alt key, called Option on macOS.
AltRight,
/// The left Meta key: Command on macOS, the Windows key elsewhere.
MetaLeft,
/// The right Meta key: Command on macOS, the Windows key elsewhere.
MetaRight,
/// The minus and underscore key on US layouts.
Minus,
/// The equals and plus key on US layouts.
Equal,
/// The left square bracket key on US layouts.
BracketLeft,
/// The right square bracket key on US layouts.
BracketRight,
/// The backslash key on US layouts.
Backslash,
/// The semicolon and colon key on US layouts.
Semicolon,
/// The apostrophe and quotation mark key on US layouts.
Quote,
/// The comma key on US layouts.
Comma,
/// The period key on US layouts.
Period,
/// The forward slash key on US layouts.
Slash,
/// The grave accent and tilde key on US layouts, left of the 1 key.
Backquote,
/// Key not recognized or not mapped.
Unknown,
}
const DOM_KEY_CODES: [(&str, KeyCode); 82] = [
("KeyA", KeyCode::A),
("KeyB", KeyCode::B),
("KeyC", KeyCode::C),
("KeyD", KeyCode::D),
("KeyE", KeyCode::E),
("KeyF", KeyCode::F),
("KeyG", KeyCode::G),
("KeyH", KeyCode::H),
("KeyI", KeyCode::I),
("KeyJ", KeyCode::J),
("KeyK", KeyCode::K),
("KeyL", KeyCode::L),
("KeyM", KeyCode::M),
("KeyN", KeyCode::N),
("KeyO", KeyCode::O),
("KeyP", KeyCode::P),
("KeyQ", KeyCode::Q),
("KeyR", KeyCode::R),
("KeyS", KeyCode::S),
("KeyT", KeyCode::T),
("KeyU", KeyCode::U),
("KeyV", KeyCode::V),
("KeyW", KeyCode::W),
("KeyX", KeyCode::X),
("KeyY", KeyCode::Y),
("KeyZ", KeyCode::Z),
("Digit0", KeyCode::Digit0),
("Digit1", KeyCode::Digit1),
("Digit2", KeyCode::Digit2),
("Digit3", KeyCode::Digit3),
("Digit4", KeyCode::Digit4),
("Digit5", KeyCode::Digit5),
("Digit6", KeyCode::Digit6),
("Digit7", KeyCode::Digit7),
("Digit8", KeyCode::Digit8),
("Digit9", KeyCode::Digit9),
("F1", KeyCode::F1),
("F2", KeyCode::F2),
("F3", KeyCode::F3),
("F4", KeyCode::F4),
("F5", KeyCode::F5),
("F6", KeyCode::F6),
("F7", KeyCode::F7),
("F8", KeyCode::F8),
("F9", KeyCode::F9),
("F10", KeyCode::F10),
("F11", KeyCode::F11),
("F12", KeyCode::F12),
("ArrowUp", KeyCode::ArrowUp),
("ArrowDown", KeyCode::ArrowDown),
("ArrowLeft", KeyCode::ArrowLeft),
("ArrowRight", KeyCode::ArrowRight),
("Home", KeyCode::Home),
("End", KeyCode::End),
("PageUp", KeyCode::PageUp),
("PageDown", KeyCode::PageDown),
("Backspace", KeyCode::Backspace),
("Delete", KeyCode::Delete),
("Enter", KeyCode::Enter),
("NumpadEnter", KeyCode::Enter),
("Tab", KeyCode::Tab),
("Space", KeyCode::Space),
("Escape", KeyCode::Escape),
("ShiftLeft", KeyCode::ShiftLeft),
("ShiftRight", KeyCode::ShiftRight),
("ControlLeft", KeyCode::ControlLeft),
("ControlRight", KeyCode::ControlRight),
("AltLeft", KeyCode::AltLeft),
("AltRight", KeyCode::AltRight),
("MetaLeft", KeyCode::MetaLeft),
("MetaRight", KeyCode::MetaRight),
("Minus", KeyCode::Minus),
("Equal", KeyCode::Equal),
("BracketLeft", KeyCode::BracketLeft),
("BracketRight", KeyCode::BracketRight),
("Backslash", KeyCode::Backslash),
("Semicolon", KeyCode::Semicolon),
("Quote", KeyCode::Quote),
("Comma", KeyCode::Comma),
("Period", KeyCode::Period),
("Slash", KeyCode::Slash),
("Backquote", KeyCode::Backquote),
];
impl KeyCode {
/// The key a W3C `KeyboardEvent.code` value names, such as `"KeyA"`,
/// `"ArrowUp"` or `"ShiftLeft"`; `"NumpadEnter"` is [`KeyCode::Enter`]. A
/// code with no counterpart here is [`KeyCode::Unknown`].
pub fn from_dom_code(code: &str) -> Self {
DOM_KEY_CODES
.iter()
.find(|(name, _)| *name == code)
.map_or(Self::Unknown, |(_, key)| *key)
}
}
/// A keyboard input event.
///
/// Contains information about which key was pressed/released,
/// the text it produces (if any), and modifier state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
/// The physical key that was pressed.
pub key_code: KeyCode,
/// The text produced by this key press (may be empty for non-character keys).
/// This accounts for keyboard layout and modifiers (e.g., Shift+A = "A").
pub text: String,
/// Current state of modifier keys.
pub modifiers: Modifiers,
/// Type of event (down or up).
pub event_type: KeyEventType,
}
impl KeyEvent {
/// Creates a new key event.
pub fn new(
key_code: KeyCode,
text: impl Into<String>,
modifiers: Modifiers,
event_type: KeyEventType,
) -> Self {
Self {
key_code,
text: text.into(),
modifiers,
event_type,
}
}
/// Creates a key down event with the given key code and text.
pub fn key_down(key_code: KeyCode, text: impl Into<String>) -> Self {
Self::new(key_code, text, Modifiers::NONE, KeyEventType::KeyDown)
}
/// Creates a key down event with modifiers.
pub fn key_down_with_modifiers(
key_code: KeyCode,
text: impl Into<String>,
modifiers: Modifiers,
) -> Self {
Self::new(key_code, text, modifiers, KeyEventType::KeyDown)
}
/// Returns true if this is a key down event.
pub fn is_key_down(&self) -> bool {
self.event_type == KeyEventType::KeyDown
}
/// Returns true if this key produces printable text.
pub fn has_text(&self) -> bool {
!self.text.is_empty()
}
}
impl fmt::Display for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"KeyEvent({:?}, text=\"{}\", {:?})",
self.key_code, self.text, self.event_type
)
}
}
#[cfg(test)]
#[path = "tests/key_event_tests.rs"]
mod tests;