Skip to content

Commit 3716846

Browse files
committed
Added API to map Ctrl+letter to a Unicode char.
Useful if you want Ctrl+C to be ETX (U+0003). It's optional, so you can handle ctrl up/down events manually if you like.
1 parent a9b5304 commit 3716846

File tree

2 files changed

+146
-45
lines changed

2 files changed

+146
-45
lines changed

src/layouts.rs

+83-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{DecodedKey, KeyCode, KeyboardLayout, Modifiers};
1+
use super::{DecodedKey, KeyCode, KeyboardLayout, Modifiers, HandleControlPlusLetter};
22

33
/// A standard United States 101-key (or 104-key including Windows keys) keyboard.
44
/// Has a 1-row high Enter key, with Backslash above.
@@ -9,7 +9,8 @@ pub struct Us104Key;
99
pub struct Uk105Key;
1010

1111
impl KeyboardLayout for Us104Key {
12-
fn map_keycode(keycode: KeyCode, modifiers: &Modifiers) -> DecodedKey {
12+
fn map_keycode(keycode: KeyCode, modifiers: &Modifiers, handle_ctrl: HandleControlPlusLetter) -> DecodedKey {
13+
let map_to_unicode = handle_ctrl == HandleControlPlusLetter::MapToUnicode;
1314
match keycode {
1415
KeyCode::BackTick => {
1516
if modifiers.is_shifted() {
@@ -106,70 +107,90 @@ impl KeyboardLayout for Us104Key {
106107
KeyCode::Backspace => DecodedKey::Unicode(0x08.into()),
107108
KeyCode::Tab => DecodedKey::Unicode(0x09.into()),
108109
KeyCode::Q => {
109-
if modifiers.is_shifted() {
110+
if map_to_unicode && modifiers.is_ctrl() {
111+
DecodedKey::Unicode('\u{0011}')
112+
} else if modifiers.is_caps() {
110113
DecodedKey::Unicode('Q')
111114
} else {
112115
DecodedKey::Unicode('q')
113116
}
114117
}
115118
KeyCode::W => {
116-
if modifiers.is_shifted() {
119+
if map_to_unicode && modifiers.is_ctrl() {
120+
DecodedKey::Unicode('\u{0017}')
121+
} else if modifiers.is_caps() {
117122
DecodedKey::Unicode('W')
118123
} else {
119124
DecodedKey::Unicode('w')
120125
}
121126
}
122127
KeyCode::E => {
123-
if modifiers.is_shifted() {
128+
if map_to_unicode && modifiers.is_ctrl() {
129+
DecodedKey::Unicode('\u{0005}')
130+
} else if modifiers.is_caps() {
124131
DecodedKey::Unicode('E')
125132
} else {
126133
DecodedKey::Unicode('e')
127134
}
128135
}
129136
KeyCode::R => {
130-
if modifiers.is_shifted() {
137+
if map_to_unicode && modifiers.is_ctrl() {
138+
DecodedKey::Unicode('\u{0012}')
139+
} else if modifiers.is_caps() {
131140
DecodedKey::Unicode('R')
132141
} else {
133142
DecodedKey::Unicode('r')
134143
}
135144
}
136145
KeyCode::T => {
137-
if modifiers.is_shifted() {
146+
if map_to_unicode && modifiers.is_ctrl() {
147+
DecodedKey::Unicode('\u{0014}')
148+
} else if modifiers.is_caps() {
138149
DecodedKey::Unicode('T')
139150
} else {
140151
DecodedKey::Unicode('t')
141152
}
142153
}
143154
KeyCode::Y => {
144-
if modifiers.is_shifted() {
155+
if map_to_unicode && modifiers.is_ctrl() {
156+
DecodedKey::Unicode('\u{0019}')
157+
} else if modifiers.is_caps() {
145158
DecodedKey::Unicode('Y')
146159
} else {
147160
DecodedKey::Unicode('y')
148161
}
149162
}
150163
KeyCode::U => {
151-
if modifiers.is_shifted() {
164+
if map_to_unicode && modifiers.is_ctrl() {
165+
DecodedKey::Unicode('\u{0015}')
166+
} else if modifiers.is_caps() {
152167
DecodedKey::Unicode('U')
153168
} else {
154169
DecodedKey::Unicode('u')
155170
}
156171
}
157172
KeyCode::I => {
158-
if modifiers.is_shifted() {
173+
if map_to_unicode && modifiers.is_ctrl() {
174+
DecodedKey::Unicode('\u{0009}')
175+
} else if modifiers.is_caps() {
159176
DecodedKey::Unicode('I')
160177
} else {
161178
DecodedKey::Unicode('i')
162179
}
163180
}
164181
KeyCode::O => {
165-
if modifiers.is_shifted() {
182+
if map_to_unicode && modifiers.is_ctrl() {
183+
DecodedKey::Unicode('\u{000F}')
184+
} else if modifiers.is_caps() {
166185
DecodedKey::Unicode('O')
167186
} else {
168187
DecodedKey::Unicode('o')
169188
}
170189
}
171190
KeyCode::P => {
172-
if modifiers.is_shifted() {
191+
if map_to_unicode && modifiers.is_ctrl() {
192+
DecodedKey::Unicode('\u{0010}')
193+
} else if modifiers.is_caps() {
173194
DecodedKey::Unicode('P')
174195
} else {
175196
DecodedKey::Unicode('p')
@@ -197,63 +218,81 @@ impl KeyboardLayout for Us104Key {
197218
}
198219
}
199220
KeyCode::A => {
200-
if modifiers.is_shifted() {
221+
if map_to_unicode && modifiers.is_ctrl() {
222+
DecodedKey::Unicode('\u{0001}')
223+
} else if modifiers.is_caps() {
201224
DecodedKey::Unicode('A')
202225
} else {
203226
DecodedKey::Unicode('a')
204227
}
205228
}
206229
KeyCode::S => {
207-
if modifiers.is_shifted() {
230+
if map_to_unicode && modifiers.is_ctrl() {
231+
DecodedKey::Unicode('\u{0013}')
232+
} else if modifiers.is_caps() {
208233
DecodedKey::Unicode('S')
209234
} else {
210235
DecodedKey::Unicode('s')
211236
}
212237
}
213238
KeyCode::D => {
214-
if modifiers.is_shifted() {
239+
if map_to_unicode && modifiers.is_ctrl() {
240+
DecodedKey::Unicode('\u{0004}')
241+
} else if modifiers.is_caps() {
215242
DecodedKey::Unicode('D')
216243
} else {
217244
DecodedKey::Unicode('d')
218245
}
219246
}
220247
KeyCode::F => {
221-
if modifiers.is_shifted() {
248+
if map_to_unicode && modifiers.is_ctrl() {
249+
DecodedKey::Unicode('\u{0006}')
250+
} else if modifiers.is_caps() {
222251
DecodedKey::Unicode('F')
223252
} else {
224253
DecodedKey::Unicode('f')
225254
}
226255
}
227256
KeyCode::G => {
228-
if modifiers.is_shifted() {
257+
if map_to_unicode && modifiers.is_ctrl() {
258+
DecodedKey::Unicode('\u{0007}')
259+
} else if modifiers.is_caps() {
229260
DecodedKey::Unicode('G')
230261
} else {
231262
DecodedKey::Unicode('g')
232263
}
233264
}
234265
KeyCode::H => {
235-
if modifiers.is_shifted() {
266+
if map_to_unicode && modifiers.is_ctrl() {
267+
DecodedKey::Unicode('\u{0008}')
268+
} else if modifiers.is_caps() {
236269
DecodedKey::Unicode('H')
237270
} else {
238271
DecodedKey::Unicode('h')
239272
}
240273
}
241274
KeyCode::J => {
242-
if modifiers.is_shifted() {
275+
if map_to_unicode && modifiers.is_ctrl() {
276+
DecodedKey::Unicode('\u{000A}')
277+
} else if modifiers.is_caps() {
243278
DecodedKey::Unicode('J')
244279
} else {
245280
DecodedKey::Unicode('j')
246281
}
247282
}
248283
KeyCode::K => {
249-
if modifiers.is_shifted() {
284+
if map_to_unicode && modifiers.is_ctrl() {
285+
DecodedKey::Unicode('\u{000B}')
286+
} else if modifiers.is_caps() {
250287
DecodedKey::Unicode('K')
251288
} else {
252289
DecodedKey::Unicode('k')
253290
}
254291
}
255292
KeyCode::L => {
256-
if modifiers.is_shifted() {
293+
if map_to_unicode && modifiers.is_ctrl() {
294+
DecodedKey::Unicode('\u{000C}')
295+
} else if modifiers.is_caps() {
257296
DecodedKey::Unicode('L')
258297
} else {
259298
DecodedKey::Unicode('l')
@@ -276,49 +315,63 @@ impl KeyboardLayout for Us104Key {
276315
// Enter gives LF, not CRLF or CR
277316
KeyCode::Enter => DecodedKey::Unicode(10.into()),
278317
KeyCode::Z => {
279-
if modifiers.is_shifted() {
318+
if map_to_unicode && modifiers.is_ctrl() {
319+
DecodedKey::Unicode('\u{001A}')
320+
} else if modifiers.is_caps() {
280321
DecodedKey::Unicode('Z')
281322
} else {
282323
DecodedKey::Unicode('z')
283324
}
284325
}
285326
KeyCode::X => {
286-
if modifiers.is_shifted() {
327+
if map_to_unicode && modifiers.is_ctrl() {
328+
DecodedKey::Unicode('\u{0018}')
329+
} else if modifiers.is_caps() {
287330
DecodedKey::Unicode('X')
288331
} else {
289332
DecodedKey::Unicode('x')
290333
}
291334
}
292335
KeyCode::C => {
293-
if modifiers.is_shifted() {
336+
if map_to_unicode && modifiers.is_ctrl() {
337+
DecodedKey::Unicode('\u{0003}')
338+
} else if modifiers.is_caps() {
294339
DecodedKey::Unicode('C')
295340
} else {
296341
DecodedKey::Unicode('c')
297342
}
298343
}
299344
KeyCode::V => {
300-
if modifiers.is_shifted() {
345+
if map_to_unicode && modifiers.is_ctrl() {
346+
DecodedKey::Unicode('\u{0016}')
347+
} else if modifiers.is_caps() {
301348
DecodedKey::Unicode('V')
302349
} else {
303350
DecodedKey::Unicode('v')
304351
}
305352
}
306353
KeyCode::B => {
307-
if modifiers.is_shifted() {
354+
if map_to_unicode && modifiers.is_ctrl() {
355+
DecodedKey::Unicode('\u{0002}')
356+
} else if modifiers.is_caps() {
308357
DecodedKey::Unicode('B')
309358
} else {
310359
DecodedKey::Unicode('b')
311360
}
312361
}
313362
KeyCode::N => {
314-
if modifiers.is_shifted() {
363+
if map_to_unicode && modifiers.is_ctrl() {
364+
DecodedKey::Unicode('\u{000E}')
365+
} else if modifiers.is_caps() {
315366
DecodedKey::Unicode('N')
316367
} else {
317368
DecodedKey::Unicode('n')
318369
}
319370
}
320371
KeyCode::M => {
321-
if modifiers.is_shifted() {
372+
if map_to_unicode && modifiers.is_ctrl() {
373+
DecodedKey::Unicode('\u{000D}')
374+
} else if modifiers.is_caps() {
322375
DecodedKey::Unicode('M')
323376
} else {
324377
DecodedKey::Unicode('m')
@@ -429,7 +482,7 @@ impl KeyboardLayout for Us104Key {
429482
}
430483

431484
impl KeyboardLayout for Uk105Key {
432-
fn map_keycode(keycode: KeyCode, modifiers: &Modifiers) -> DecodedKey {
485+
fn map_keycode(keycode: KeyCode, modifiers: &Modifiers, handle_ctrl: HandleControlPlusLetter) -> DecodedKey {
433486
match keycode {
434487
KeyCode::BackTick => {
435488
if modifiers.alt_gr {
@@ -477,7 +530,7 @@ impl KeyboardLayout for Uk105Key {
477530
DecodedKey::Unicode('#')
478531
}
479532
}
480-
e => <Us104Key as KeyboardLayout>::map_keycode(e, modifiers),
533+
e => <Us104Key as KeyboardLayout>::map_keycode(e, modifiers, handle_ctrl),
481534
}
482535
}
483536
}

0 commit comments

Comments
 (0)