-
Notifications
You must be signed in to change notification settings - Fork 499
/
keyboard_manager.rs
282 lines (262 loc) · 9.8 KB
/
keyboard_manager.rs
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
use crate::{
bridge::{SerialCommand, UiCommand},
event_aggregator::EVENT_AGGREGATOR,
settings::SETTINGS,
window::KeyboardSettings,
};
use winit::{
event::{ElementState, Event, Ime, KeyEvent, WindowEvent},
keyboard::{Key, Key::Dead},
platform::modifier_supplement::KeyEventExtModifierSupplement,
};
enum InputEvent {
KeyEvent(KeyEvent),
ImeInput(String),
}
pub struct KeyboardManager {
shift: bool,
ctrl: bool,
alt: bool,
prev_dead_key: Option<char>,
logo: bool,
ignore_input_this_frame: bool,
queued_input_events: Vec<InputEvent>,
}
impl KeyboardManager {
pub fn new() -> KeyboardManager {
KeyboardManager {
shift: false,
ctrl: false,
alt: false,
prev_dead_key: None,
logo: false,
ignore_input_this_frame: false,
queued_input_events: Vec::new(),
}
}
pub fn handle_event(&mut self, event: &Event<()>) {
match event {
Event::WindowEvent {
event: WindowEvent::Focused(_focused),
..
} => {
// When window is just focused or lost it's focus, ignore keyboard events
// that were submitted this frame
self.ignore_input_this_frame = true;
}
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
event: key_event, ..
},
..
} => {
// Store the event so that we can ignore it properly if the window was just
// focused.
self.queued_input_events
.push(InputEvent::KeyEvent(key_event.clone()));
}
Event::WindowEvent {
event: WindowEvent::Ime(Ime::Commit(string)),
..
} => {
self.queued_input_events
.push(InputEvent::ImeInput(string.to_string()));
}
Event::WindowEvent {
event: WindowEvent::ModifiersChanged(modifiers),
..
} => {
// Record the modifier states so that we can properly add them to the keybinding
// text
let state = modifiers.state();
self.shift = state.shift_key();
self.ctrl = state.control_key();
self.alt = state.alt_key();
self.logo = state.super_key();
}
Event::MainEventsCleared => {
// If the window wasn't just focused.
if !self.should_ignore_input() {
// And we have a keyboard event this frame
for input_event in self.queued_input_events.iter() {
let mut next_dead_key = self.prev_dead_key;
match input_event {
InputEvent::KeyEvent(key_event) => {
// And a key was pressed
if key_event.state == ElementState::Pressed {
if let Some(keybinding) = self.maybe_get_keybinding(key_event) {
EVENT_AGGREGATOR.send(UiCommand::Serial(
SerialCommand::Keyboard(keybinding),
));
}
next_dead_key = None;
} else if key_event.state == ElementState::Released {
// dead key detect here
if let Dead(dead_key) = key_event.logical_key {
// should wait for the next input text_with_all_modifiers, and ignore the next ime.
next_dead_key = dead_key;
}
}
}
InputEvent::ImeInput(raw_input) => {
if self.prev_dead_key.is_none() {
EVENT_AGGREGATOR.send(UiCommand::Serial(
SerialCommand::Keyboard(raw_input.to_string()),
));
}
}
}
self.prev_dead_key = next_dead_key;
}
}
// Regardless of whether this was a valid keyboard input or not, rest ignoring and
// whatever event was queued.
self.ignore_input_this_frame = false;
self.queued_input_events.clear();
}
_ => {}
}
}
fn should_ignore_input(&self) -> bool {
let settings = SETTINGS.get::<KeyboardSettings>();
self.ignore_input_this_frame || (self.logo && !settings.use_logo)
}
fn maybe_get_keybinding(&self, key_event: &KeyEvent) -> Option<String> {
// Determine if this key event represents a key which won't ever
// present text.
if let Some(key_text) = is_control_key(&key_event.logical_key) {
if self.prev_dead_key.is_some() {
//recover dead key to normal character
let real_char = String::from(self.prev_dead_key.unwrap());
Some(real_char + &self.format_special_key(true, key_text))
} else {
Some(self.format_special_key(true, key_text))
}
} else {
let key_text = if self.prev_dead_key.is_none() {
key_event.text.as_ref().map(|text| text.as_str())
} else {
key_event.text_with_all_modifiers()
};
if let Some(original_key_text) = key_text {
let mut key_text = original_key_text.to_string();
if self.alt {
if let Some(modify) = key_event_text(key_event) {
key_text = modify;
}
}
// This is not a control key, so we rely upon winit to determine if
// this is a deadkey or not.
let keybinding_string =
if let Some((escaped_text, use_shift)) = is_special(&key_text) {
self.format_special_key(use_shift, escaped_text)
} else {
self.format_normal_key(&key_text)
};
Some(keybinding_string)
} else {
None
}
}
}
fn format_special_key(&self, use_shift: bool, text: &str) -> String {
let modifiers = self.format_modifier_string(use_shift);
format!("<{modifiers}{text}>")
}
fn format_normal_key(&self, text: &str) -> String {
let has_modifier = self.ctrl || use_alt(self.alt) || self.logo;
// use shift only if `text` is alphabetic
let is_alphabetic = text.chars().all(char::is_alphabetic);
if has_modifier {
self.format_special_key(is_alphabetic, text)
} else {
text.to_string()
}
}
pub fn format_modifier_string(&self, use_shift: bool) -> String {
let shift = or_empty(self.shift && use_shift, "S-");
let ctrl = or_empty(self.ctrl, "C-");
let alt = or_empty(use_alt(self.alt), "M-");
let logo = or_empty(self.logo, "D-");
shift.to_owned() + ctrl + alt + logo
}
}
#[cfg(not(target_os = "macos"))]
fn use_alt(alt: bool) -> bool {
alt
}
// The option or alt key is used on Macos for character set changes
// and does not operate the same as other systems.
#[cfg(target_os = "macos")]
fn use_alt(alt: bool) -> bool {
let settings = SETTINGS.get::<KeyboardSettings>();
settings.macos_alt_is_meta && alt
}
#[cfg(not(target_os = "macos"))]
fn key_event_text(key_event: &KeyEvent) -> Option<String> {
key_event
.key_without_modifiers()
.to_text()
.map(|text| text.to_string())
}
#[cfg(target_os = "macos")]
fn key_event_text(key_event: &KeyEvent) -> Option<String> {
let settings = SETTINGS.get::<KeyboardSettings>();
if settings.macos_alt_is_meta {
key_event.text.as_ref().map(|text| text.to_string())
} else {
key_event
.text_with_all_modifiers()
.map(|text| text.to_string())
}
}
fn or_empty(condition: bool, text: &str) -> &str {
if condition {
text
} else {
""
}
}
fn is_control_key(key: &Key) -> Option<&'static str> {
match key {
Key::Backspace => Some("BS"),
Key::Escape => Some("Esc"),
Key::Delete => Some("Del"),
Key::ArrowUp => Some("Up"),
Key::ArrowDown => Some("Down"),
Key::ArrowLeft => Some("Left"),
Key::ArrowRight => Some("Right"),
Key::F1 => Some("F1"),
Key::F2 => Some("F2"),
Key::F3 => Some("F3"),
Key::F4 => Some("F4"),
Key::F5 => Some("F5"),
Key::F6 => Some("F6"),
Key::F7 => Some("F7"),
Key::F8 => Some("F8"),
Key::F9 => Some("F9"),
Key::F10 => Some("F10"),
Key::F11 => Some("F11"),
Key::F12 => Some("F12"),
Key::Insert => Some("Insert"),
Key::Home => Some("Home"),
Key::End => Some("End"),
Key::PageUp => Some("PageUp"),
Key::PageDown => Some("PageDown"),
Key::Tab => Some("Tab"),
_ => None,
}
}
// returns (`escaped_text`, `use_shift`)
fn is_special(text: &str) -> Option<(&str, bool)> {
match text {
" " => Some(("Space", true)),
"<" => Some(("lt", false)),
"\\" => Some(("Bslash", false)),
"|" => Some(("Bar", false)),
"\t" => Some(("Tab", true)),
"\n" | "\r" => Some(("CR", true)),
_ => None,
}
}