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
//! Flutter counterpart: `engine/src/flutter/lib/ui/key.dart`.
use std::fmt::{self, Display};
use std::time::Duration;
/// The type of a key event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyEventType {
/// The key is pressed.
Down,
/// The key is released.
Up,
/// The key is held, causing a repeated key input.
Repeat,
}
impl KeyEventType {
/// A human readable name, used by [`KeyData`]'s textual descriptions.
pub fn label(&self) -> &'static str {
match self {
KeyEventType::Down => "Key Down",
KeyEventType::Up => "Key Up",
KeyEventType::Repeat => "Key Repeat",
}
}
}
/// The source device for the key event.
///
/// Not all platforms supply an accurate type.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyEventDeviceType {
/// The device is a keyboard.
#[default]
Keyboard,
/// The device is a directional pad on something like a television remote
/// control or similar.
DirectionalPad,
/// The device is a gamepad button
Gamepad,
/// The device is a joystick button
Joystick,
/// The device is a device connected to an HDMI bus.
Hdmi,
}
impl KeyEventDeviceType {
/// A human readable name, used by [`KeyData`]'s textual descriptions.
pub fn label(&self) -> &'static str {
match self {
KeyEventDeviceType::Keyboard => "Keyboard",
KeyEventDeviceType::DirectionalPad => "Directional Pad",
KeyEventDeviceType::Gamepad => "Gamepad",
KeyEventDeviceType::Joystick => "Joystick",
KeyEventDeviceType::Hdmi => "HDMI",
}
}
}
/// Information about a key event.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct KeyData {
/// Time of event dispatch, relative to an arbitrary timeline.
///
/// For synthesized events, the [`time_stamp`](Self::time_stamp) might not be
/// the actual time that the key press or release happens.
pub time_stamp: Duration,
/// The type of the event.
pub event_type: KeyEventType,
/// Describes what type of device (keyboard, directional pad, etc.) this event
/// originated from.
pub device_type: KeyEventDeviceType,
/// The key code for the physical key that has changed.
pub physical: u64,
/// The key code for the logical key that has changed.
pub logical: u64,
/// Character input from the event.
///
/// Ignored for up events.
pub character: Option<String>,
/// If [`synthesized`](Self::synthesized) is true, this event does not
/// correspond to a native event.
///
/// Although most of Flutter's keyboard events are transformed from native
/// events, some events are not based on native events, and are synthesized
/// only to conform Flutter's key event model (as documented in the
/// `HardwareKeyboard` class in the framework).
///
/// For example, some key downs or ups might be lost when the window loses
/// focus. Some platforms provide ways to query whether a key is being held.
/// If the embedder detects an inconsistency between its internal record and
/// the state returned by the system, the embedder will synthesize a
/// corresponding event to synchronize the state without breaking the event
/// model.
///
/// As another example, macOS treats CapsLock in a special way by sending down
/// and up events at the down of alternate presses to indicate the direction
/// in which the lock is toggled instead of that the physical key is going. A
/// macOS embedder should normalize the behavior by converting a native down
/// event into a down event followed immediately by a synthesized up event,
/// and the native up event also into a down event followed immediately by a
/// synthesized up event.
///
/// Synthesized events do not have a trustworthy
/// [`time_stamp`](Self::time_stamp), and should not be processed as if the
/// key actually went down or up at the time of the callback.
///
/// A key repeat event is never synthesized.
pub synthesized: bool,
}
impl Default for KeyData {
fn default() -> KeyData {
KeyData {
time_stamp: Duration::ZERO,
event_type: KeyEventType::Down,
device_type: KeyEventDeviceType::Keyboard,
physical: 0,
logical: 0,
character: None,
synthesized: false,
}
}
}
impl KeyData {
/// Returns the bits that are not included in the value mask, shifted to the
/// right.
///
/// For example, if the input is 0x12abcdabcd, then the result is 0x12.
fn non_value_bits(n: u64) -> u64 {
const VALUE_MASK_WIDTH: u32 = 32;
// Dart limits the non-value bits to what a JavaScript number can hold.
const MAX_SAFE_INTEGER_WIDTH: u32 = 52;
const NON_VALUE_MASK: u64 = (1 << (MAX_SAFE_INTEGER_WIDTH - VALUE_MASK_WIDTH)) - 1;
(n >> VALUE_MASK_WIDTH) & NON_VALUE_MASK
}
fn logical_to_string(&self) -> String {
let plane_description = match KeyData::non_value_bits(self.logical) & 0x0ff {
0x000 => " (Unicode)",
0x001 => " (Unprintable)",
0x002 => " (Flutter)",
0x011 => " (Android)",
0x012 => " (Fuchsia)",
0x013 => " (iOS)",
0x014 => " (macOS)",
0x015 => " (GTK)",
0x016 => " (Windows)",
0x017 => " (Web)",
0x018 => " (GLFW)",
_ => "",
};
format!("0x{:x}{}", self.logical, plane_description)
}
fn escape_character(&self) -> String {
let Some(character) = &self.character else {
return "<none>".to_owned();
};
match character.as_str() {
"\n" => r#""\n""#.to_owned(),
"\t" => r#""\t""#.to_owned(),
"\r" => r#""\r""#.to_owned(),
"\u{8}" => r#""\b""#.to_owned(),
"\u{c}" => r#""\f""#.to_owned(),
_ => format!("\"{character}\""),
}
}
fn quoted_char_code(&self) -> String {
let Some(character) = &self.character else {
return String::new();
};
let hex_chars: Vec<String> = character
.encode_utf16()
.map(|code| format!("{code:02x}"))
.collect();
format!(" (0x{})", hex_chars.join(" "))
}
/// Returns a complete textual description of the information in this object.
pub fn to_string_full(&self) -> String {
format!(
"KeyData(type: {}, \
deviceType: {}, \
timeStamp: {:?}, \
physical: 0x{:x}, \
logical: 0x{:x}, \
character: {}, \
synthesized: {}\
)",
self.event_type.label(),
self.device_type.label(),
self.time_stamp,
self.physical,
self.logical,
self.escape_character(),
self.synthesized,
)
}
}
impl Display for KeyData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"KeyData({}, physical: 0x{:x}, logical: {}, character: {}{}{}",
self.event_type.label(),
self.physical,
self.logical_to_string(),
self.escape_character(),
self.quoted_char_code(),
if self.synthesized {
", synthesized"
} else {
""
},
)
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::{KeyData, KeyEventDeviceType, KeyEventType};
#[test]
fn to_string_names_the_plane_and_quotes_the_character() {
let data = KeyData {
time_stamp: Duration::from_millis(5),
event_type: KeyEventType::Down,
device_type: KeyEventDeviceType::Keyboard,
physical: 0x00070004,
logical: 0x00000000061,
character: Some("a".to_owned()),
synthesized: false,
};
assert_eq!(
data.to_string(),
"KeyData(Key Down, physical: 0x70004, logical: 0x61 (Unicode), character: \"a\" (0x61)"
);
}
#[test]
fn to_string_full_reports_every_field() {
let data = KeyData {
time_stamp: Duration::from_millis(5),
event_type: KeyEventType::Up,
device_type: KeyEventDeviceType::Gamepad,
physical: 0x00070004,
logical: 0x00100000301,
character: None,
synthesized: true,
};
assert_eq!(
data.to_string_full(),
"KeyData(type: Key Up, deviceType: Gamepad, timeStamp: 5ms, physical: 0x70004, \
logical: 0x100000301, character: <none>, synthesized: true)"
);
}
}