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
//! 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;
/// Type of keyboard event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyEventType {
/// Key was pressed down.
KeyDown,
/// Key was released.
KeyUp,
}
/// Modifier keys state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Modifiers {
/// Shift key is pressed.
pub shift: bool,
/// Control key is pressed (Cmd on macOS).
pub ctrl: bool,
/// Alt key is pressed (Option on macOS).
pub alt: bool,
/// Meta/Super key is pressed (Windows key, Cmd on macOS).
pub meta: bool,
}
impl Modifiers {
/// No modifiers pressed.
pub const NONE: Modifiers = Modifiers {
shift: false,
ctrl: false,
alt: false,
meta: false,
};
/// Returns true if any modifier is pressed.
pub fn any(&self) -> bool {
self.shift || self.ctrl || self.alt || self.meta
}
/// Returns true if Ctrl (or Cmd on macOS) is pressed.
pub fn command_or_ctrl(&self) -> bool {
#[cfg(target_os = "macos")]
{
self.meta
}
#[cfg(not(target_os = "macos"))]
{
self.ctrl
}
}
}
/// 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 {
// Letters
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
// Numbers
Digit0,
Digit1,
Digit2,
Digit3,
Digit4,
Digit5,
Digit6,
Digit7,
Digit8,
Digit9,
// Function keys
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
// Navigation
ArrowUp,
ArrowDown,
ArrowLeft,
ArrowRight,
Home,
End,
PageUp,
PageDown,
// Editing
Backspace,
Delete,
Enter,
Tab,
Space,
Escape,
// Modifiers (for completeness, usually detected via Modifiers struct)
ShiftLeft,
ShiftRight,
ControlLeft,
ControlRight,
AltLeft,
AltRight,
MetaLeft,
MetaRight,
// Punctuation and symbols
Minus,
Equal,
BracketLeft,
BracketRight,
Backslash,
Semicolon,
Quote,
Comma,
Period,
Slash,
Backquote,
/// Key not recognized or not mapped.
Unknown,
}
/// 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 is a key up event.
pub fn is_key_up(&self) -> bool {
self.event_type == KeyEventType::KeyUp
}
/// 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)]
mod tests {
use super::*;
#[test]
fn key_event_creation() {
let event = KeyEvent::key_down(KeyCode::A, "a");
assert_eq!(event.key_code, KeyCode::A);
assert_eq!(event.text, "a");
assert!(event.is_key_down());
assert!(event.has_text());
}
#[test]
fn key_event_with_modifiers() {
let modifiers = Modifiers {
shift: true,
ctrl: false,
alt: false,
meta: false,
};
let event = KeyEvent::key_down_with_modifiers(KeyCode::A, "A", modifiers);
assert_eq!(event.text, "A");
assert!(event.modifiers.shift);
}
#[test]
fn backspace_has_no_text() {
let event = KeyEvent::key_down(KeyCode::Backspace, "");
assert!(!event.has_text());
}
#[test]
fn modifiers_any() {
assert!(!Modifiers::NONE.any());
assert!(Modifiers {
shift: true,
..Modifiers::NONE
}
.any());
}
}