-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathkeyboard.rs
1286 lines (1219 loc) · 52.2 KB
/
keyboard.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
char,
ffi::OsString,
mem::MaybeUninit,
os::windows::ffi::OsStringExt,
sync::{
atomic::{AtomicU32, Ordering::Relaxed},
Mutex, MutexGuard,
},
};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, WPARAM},
System::SystemServices::LANG_KOREAN,
UI::{
Input::KeyboardAndMouse::{
GetAsyncKeyState, GetKeyState, GetKeyboardLayout, GetKeyboardState, MapVirtualKeyExW,
MAPVK_VK_TO_VSC_EX, MAPVK_VSC_TO_VK_EX, VIRTUAL_KEY, VK_ABNT_C2, VK_ADD, VK_CAPITAL,
VK_CLEAR, VK_CONTROL, VK_DECIMAL, VK_DELETE, VK_DIVIDE, VK_DOWN, VK_END, VK_F4,
VK_HOME, VK_INSERT, VK_LCONTROL, VK_LEFT, VK_LMENU, VK_LSHIFT, VK_LWIN, VK_MENU,
VK_MULTIPLY, VK_NEXT, VK_NUMLOCK, VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3,
VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, VK_NUMPAD8, VK_NUMPAD9, VK_PRIOR,
VK_RCONTROL, VK_RETURN, VK_RIGHT, VK_RMENU, VK_RSHIFT, VK_RWIN, VK_SCROLL, VK_SHIFT,
VK_SUBTRACT, VK_UP,
},
TextServices::HKL,
WindowsAndMessaging::{
PeekMessageW, MSG, PM_NOREMOVE, WM_CHAR, WM_DEADCHAR, WM_KEYDOWN, WM_KEYFIRST,
WM_KEYLAST, WM_KEYUP, WM_KILLFOCUS, WM_SETFOCUS, WM_SYSCHAR, WM_SYSDEADCHAR,
WM_SYSKEYDOWN, WM_SYSKEYUP,
},
},
};
use smol_str::SmolStr;
use unicode_segmentation::UnicodeSegmentation;
use crate::{
event::{ElementState, KeyEvent},
keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey},
platform::scancode::PhysicalKeyExtScancode,
platform_impl::platform::{
event_loop::ProcResult,
keyboard_layout::{Layout, LayoutCache, WindowsModifiers, LAYOUT_CACHE},
loword, primarylangid, KeyEventExtra,
},
};
pub type ExScancode = u16;
pub struct MessageAsKeyEvent {
pub event: KeyEvent,
pub is_synthetic: bool,
}
/// Stores information required to make `KeyEvent`s.
///
/// A single Winit `KeyEvent` contains information which the Windows API passes to the application
/// in multiple window messages. In other words: a Winit `KeyEvent` cannot be built from a single
/// window message. Therefore, this type keeps track of certain information from previous events so
/// that a `KeyEvent` can be constructed when the last event related to a keypress is received.
///
/// `PeekMessage` is sometimes used to determine whether the next window message still belongs to the
/// current keypress. If it doesn't and the current state represents a key event waiting to be
/// dispatched, then said event is considered complete and is dispatched.
///
/// The sequence of window messages for a key press event is the following:
/// - Exactly one WM_KEYDOWN / WM_SYSKEYDOWN
/// - Zero or one WM_DEADCHAR / WM_SYSDEADCHAR
/// - Zero or more WM_CHAR / WM_SYSCHAR. These messages each come with a UTF-16 code unit which when
/// put together in the sequence they arrived in, forms the text which is the result of pressing the
/// key.
///
/// Key release messages are a bit different due to the fact that they don't contribute to
/// text input. The "sequence" only consists of one WM_KEYUP / WM_SYSKEYUP event.
pub struct KeyEventBuilder {
event_info: Mutex<Option<PartialKeyEventInfo>>,
pending: PendingEventQueue<MessageAsKeyEvent>,
}
impl Default for KeyEventBuilder {
fn default() -> Self {
KeyEventBuilder {
event_info: Mutex::new(None),
pending: Default::default(),
}
}
}
impl KeyEventBuilder {
/// Call this function for every window message.
/// Returns Some() if this window message completes a KeyEvent.
/// Returns None otherwise.
pub(crate) fn process_message(
&self,
hwnd: HWND,
msg_kind: u32,
wparam: WPARAM,
lparam: LPARAM,
result: &mut ProcResult,
) -> Vec<MessageAsKeyEvent> {
enum MatchResult {
Nothing,
TokenToRemove(PendingMessageToken),
MessagesToDispatch(Vec<MessageAsKeyEvent>),
}
let mut matcher = || -> MatchResult {
match msg_kind {
WM_SETFOCUS => {
// synthesize keydown events
let kbd_state = get_async_kbd_state();
let key_events = Self::synthesize_kbd_state(ElementState::Pressed, &kbd_state);
MatchResult::MessagesToDispatch(self.pending.complete_multi(key_events))
}
WM_KILLFOCUS => {
// sythesize keyup events
let kbd_state = get_kbd_state();
let key_events = Self::synthesize_kbd_state(ElementState::Released, &kbd_state);
MatchResult::MessagesToDispatch(self.pending.complete_multi(key_events))
}
WM_KEYDOWN | WM_SYSKEYDOWN => {
if msg_kind == WM_SYSKEYDOWN && wparam as VIRTUAL_KEY == VK_F4 {
// Don't dispatch Alt+F4 to the application.
// This is handled in `event_loop.rs`
return MatchResult::Nothing;
}
let pending_token = self.pending.add_pending();
*result = ProcResult::Value(0);
let next_msg = next_kbd_msg(hwnd);
let mut layouts = LAYOUT_CACHE.lock().unwrap();
let mut finished_event_info = Some(PartialKeyEventInfo::from_message(
wparam,
lparam,
ElementState::Pressed,
&mut layouts,
));
let mut event_info = self.event_info.lock().unwrap();
*event_info = None;
if let Some(next_msg) = next_msg {
let next_msg_kind = next_msg.message;
let next_belongs_to_this = !matches!(
next_msg_kind,
WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP
);
if next_belongs_to_this {
// The next OS event belongs to this Winit event, so let's just
// store the partial information, and add to it in the upcoming events
*event_info = finished_event_info.take();
} else {
let (_, layout) = layouts.get_current_layout();
let is_fake = {
let curr_event = finished_event_info.as_ref().unwrap();
is_current_fake(curr_event, next_msg, layout)
};
if is_fake {
finished_event_info = None;
}
}
}
if let Some(event_info) = finished_event_info {
let ev = event_info.finalize();
return MatchResult::MessagesToDispatch(self.pending.complete_pending(
pending_token,
MessageAsKeyEvent {
event: ev,
is_synthetic: false,
},
));
}
MatchResult::TokenToRemove(pending_token)
}
WM_DEADCHAR | WM_SYSDEADCHAR => {
let pending_token = self.pending.add_pending();
*result = ProcResult::Value(0);
// At this point, we know that there isn't going to be any more events related to
// this key press
let event_info = self.event_info.lock().unwrap().take().unwrap();
let ev = event_info.finalize();
MatchResult::MessagesToDispatch(self.pending.complete_pending(
pending_token,
MessageAsKeyEvent {
event: ev,
is_synthetic: false,
},
))
}
WM_CHAR | WM_SYSCHAR => {
let mut event_info = self.event_info.lock().unwrap();
if event_info.is_none() {
trace!("Received a CHAR message but no `event_info` was available. The message is probably IME, returning.");
return MatchResult::Nothing;
}
let pending_token = self.pending.add_pending();
*result = ProcResult::Value(0);
let is_high_surrogate = (0xD800..=0xDBFF).contains(&wparam);
let is_low_surrogate = (0xDC00..=0xDFFF).contains(&wparam);
let is_utf16 = is_high_surrogate || is_low_surrogate;
if is_utf16 {
if let Some(ev_info) = event_info.as_mut() {
ev_info.utf16parts.push(wparam as u16);
}
} else {
// In this case, wparam holds a UTF-32 character.
// Let's encode it as UTF-16 and append it to the end of `utf16parts`
let utf16parts = match event_info.as_mut() {
Some(ev_info) => &mut ev_info.utf16parts,
None => {
warn!("The event_info was None when it was expected to be some");
return MatchResult::TokenToRemove(pending_token);
}
};
let start_offset = utf16parts.len();
let new_size = utf16parts.len() + 2;
utf16parts.resize(new_size, 0);
if let Some(ch) = char::from_u32(wparam as u32) {
let encode_len = ch.encode_utf16(&mut utf16parts[start_offset..]).len();
let new_size = start_offset + encode_len;
utf16parts.resize(new_size, 0);
}
}
// It's important that we unlock the mutex, and create the pending event token before
// calling `next_msg`
std::mem::drop(event_info);
let next_msg = next_kbd_msg(hwnd);
let more_char_coming = next_msg
.map(|m| matches!(m.message, WM_CHAR | WM_SYSCHAR))
.unwrap_or(false);
if more_char_coming {
// No need to produce an event just yet, because there are still more characters that
// need to appended to this keyobard event
MatchResult::TokenToRemove(pending_token)
} else {
let mut event_info = self.event_info.lock().unwrap();
let mut event_info = match event_info.take() {
Some(ev_info) => ev_info,
None => {
warn!("The event_info was None when it was expected to be some");
return MatchResult::TokenToRemove(pending_token);
}
};
let mut layouts = LAYOUT_CACHE.lock().unwrap();
// It's okay to call `ToUnicode` here, because at this point the dead key
// is already consumed by the character.
let kbd_state = get_kbd_state();
let mod_state = WindowsModifiers::active_modifiers(&kbd_state);
let (_, layout) = layouts.get_current_layout();
let ctrl_on = if layout.has_alt_graph {
let alt_on = mod_state.contains(WindowsModifiers::ALT);
!alt_on && mod_state.contains(WindowsModifiers::CONTROL)
} else {
mod_state.contains(WindowsModifiers::CONTROL)
};
// If Ctrl is not pressed, just use the text with all
// modifiers because that already consumed the dead key. Otherwise,
// we would interpret the character incorrectly, missing the dead key.
if !ctrl_on {
event_info.text = PartialText::System(event_info.utf16parts.clone());
} else {
let mod_no_ctrl = mod_state.remove_only_ctrl();
let num_lock_on = kbd_state[VK_NUMLOCK as usize] & 1 != 0;
let vkey = event_info.vkey;
let physical_key = &event_info.physical_key;
let key = layout.get_key(mod_no_ctrl, num_lock_on, vkey, physical_key);
event_info.text = PartialText::Text(key.to_text().map(SmolStr::new));
}
let ev = event_info.finalize();
MatchResult::MessagesToDispatch(self.pending.complete_pending(
pending_token,
MessageAsKeyEvent {
event: ev,
is_synthetic: false,
},
))
}
}
WM_KEYUP | WM_SYSKEYUP => {
let pending_token = self.pending.add_pending();
*result = ProcResult::Value(0);
let mut layouts = LAYOUT_CACHE.lock().unwrap();
let event_info = PartialKeyEventInfo::from_message(
wparam,
lparam,
ElementState::Released,
&mut layouts,
);
// We MUST release the layout lock before calling `next_kbd_msg`, otherwise it may deadlock
drop(layouts);
// It's important that we create the pending token before reading the next message.
let next_msg = next_kbd_msg(hwnd);
let mut valid_event_info = Some(event_info);
if let Some(next_msg) = next_msg {
let mut layouts = LAYOUT_CACHE.lock().unwrap();
let (_, layout) = layouts.get_current_layout();
let is_fake = {
let event_info = valid_event_info.as_ref().unwrap();
is_current_fake(event_info, next_msg, layout)
};
if is_fake {
valid_event_info = None;
}
}
if let Some(event_info) = valid_event_info {
let event = event_info.finalize();
return MatchResult::MessagesToDispatch(self.pending.complete_pending(
pending_token,
MessageAsKeyEvent {
event,
is_synthetic: false,
},
));
}
MatchResult::TokenToRemove(pending_token)
}
_ => MatchResult::Nothing,
}
};
let matcher_result = matcher();
match matcher_result {
MatchResult::TokenToRemove(t) => self.pending.remove_pending(t),
MatchResult::MessagesToDispatch(m) => m,
MatchResult::Nothing => Vec::new(),
}
}
// Alowing nominimal_bool lint because the `is_key_pressed` macro triggers this warning
// and I don't know of another way to resolve it and also keeping the macro
#[allow(clippy::nonminimal_bool)]
fn synthesize_kbd_state(
key_state: ElementState,
kbd_state: &[u8; 256],
) -> Vec<MessageAsKeyEvent> {
let mut key_events = Vec::new();
let mut layouts = LAYOUT_CACHE.lock().unwrap();
let (locale_id, _) = layouts.get_current_layout();
macro_rules! is_key_pressed {
($vk:expr) => {
kbd_state[$vk as usize] & 0x80 != 0
};
}
// Is caps-lock active? Note that this is different from caps-lock
// being held down.
let caps_lock_on = kbd_state[VK_CAPITAL as usize] & 1 != 0;
let num_lock_on = kbd_state[VK_NUMLOCK as usize] & 1 != 0;
// We are synthesizing the press event for caps-lock first for the following reasons:
// 1. If caps-lock is *not* held down but *is* active, then we have to
// synthesize all printable keys, respecting the caps-lock state.
// 2. If caps-lock is held down, we could choose to sythesize its
// keypress after every other key, in which case all other keys *must*
// be sythesized as if the caps-lock state was be the opposite
// of what it currently is.
// --
// For the sake of simplicity we are choosing to always sythesize
// caps-lock first, and always use the current caps-lock state
// to determine the produced text
if is_key_pressed!(VK_CAPITAL) {
let event = Self::create_synthetic(
VK_CAPITAL,
key_state,
caps_lock_on,
num_lock_on,
locale_id as HKL,
&mut layouts,
);
if let Some(event) = event {
key_events.push(event);
}
}
let do_non_modifier = |key_events: &mut Vec<_>, layouts: &mut _| {
for vk in 0..256 {
match vk {
VK_CONTROL | VK_LCONTROL | VK_RCONTROL | VK_SHIFT | VK_LSHIFT | VK_RSHIFT
| VK_MENU | VK_LMENU | VK_RMENU | VK_CAPITAL => continue,
_ => (),
}
if !is_key_pressed!(vk) {
continue;
}
let event = Self::create_synthetic(
vk,
key_state,
caps_lock_on,
num_lock_on,
locale_id as HKL,
layouts,
);
if let Some(event) = event {
key_events.push(event);
}
}
};
let do_modifier = |key_events: &mut Vec<_>, layouts: &mut _| {
const CLEAR_MODIFIER_VKS: [VIRTUAL_KEY; 6] = [
VK_LCONTROL,
VK_LSHIFT,
VK_LMENU,
VK_RCONTROL,
VK_RSHIFT,
VK_RMENU,
];
for vk in CLEAR_MODIFIER_VKS.iter() {
if is_key_pressed!(*vk) {
let event = Self::create_synthetic(
*vk,
key_state,
caps_lock_on,
num_lock_on,
locale_id as HKL,
layouts,
);
if let Some(event) = event {
key_events.push(event);
}
}
}
};
// Be cheeky and sequence modifier and non-modifier
// key events such that non-modifier keys are not affected
// by modifiers (except for caps-lock)
match key_state {
ElementState::Pressed => {
do_non_modifier(&mut key_events, &mut layouts);
do_modifier(&mut key_events, &mut layouts);
}
ElementState::Released => {
do_modifier(&mut key_events, &mut layouts);
do_non_modifier(&mut key_events, &mut layouts);
}
}
key_events
}
fn create_synthetic(
vk: VIRTUAL_KEY,
key_state: ElementState,
caps_lock_on: bool,
num_lock_on: bool,
locale_id: HKL,
layouts: &mut MutexGuard<'_, LayoutCache>,
) -> Option<MessageAsKeyEvent> {
let scancode = unsafe { MapVirtualKeyExW(vk as u32, MAPVK_VK_TO_VSC_EX, locale_id) };
if scancode == 0 {
return None;
}
let scancode = scancode as ExScancode;
let physical_key = PhysicalKey::from_scancode(scancode as u32);
let mods = if caps_lock_on {
WindowsModifiers::CAPS_LOCK
} else {
WindowsModifiers::empty()
};
let layout = layouts.layouts.get(&(locale_id as u64)).unwrap();
let logical_key = layout.get_key(mods, num_lock_on, vk, &physical_key);
let key_without_modifiers =
layout.get_key(WindowsModifiers::empty(), false, vk, &physical_key);
let text = if key_state == ElementState::Pressed {
logical_key.to_text().map(SmolStr::new)
} else {
None
};
let event_info = PartialKeyEventInfo {
vkey: vk,
logical_key: PartialLogicalKey::This(logical_key.clone()),
key_without_modifiers,
key_state,
is_repeat: false,
physical_key,
location: get_location(scancode, locale_id),
utf16parts: Vec::with_capacity(8),
text: PartialText::Text(text.clone()),
};
let mut event = event_info.finalize();
event.logical_key = logical_key;
event.platform_specific.text_with_all_modifers = text;
Some(MessageAsKeyEvent {
event,
is_synthetic: true,
})
}
}
enum PartialText {
// Unicode
System(Vec<u16>),
Text(Option<SmolStr>),
}
enum PartialLogicalKey {
/// Use the text provided by the WM_CHAR messages and report that as a `Character` variant. If
/// the text consists of multiple grapheme clusters (user-precieved characters) that means that
/// dead key could not be combined with the second input, and in that case we should fall back
/// to using what would have without a dead-key input.
TextOr(Key),
/// Use the value directly provided by this variant
This(Key),
}
struct PartialKeyEventInfo {
vkey: VIRTUAL_KEY,
key_state: ElementState,
is_repeat: bool,
physical_key: PhysicalKey,
location: KeyLocation,
logical_key: PartialLogicalKey,
key_without_modifiers: Key,
/// The UTF-16 code units of the text that was produced by the keypress event.
/// This take all modifiers into account. Including CTRL
utf16parts: Vec<u16>,
text: PartialText,
}
impl PartialKeyEventInfo {
fn from_message(
wparam: WPARAM,
lparam: LPARAM,
state: ElementState,
layouts: &mut MutexGuard<'_, LayoutCache>,
) -> Self {
const NO_MODS: WindowsModifiers = WindowsModifiers::empty();
let (_, layout) = layouts.get_current_layout();
let lparam_struct = destructure_key_lparam(lparam);
let vkey = wparam as VIRTUAL_KEY;
let scancode = if lparam_struct.scancode == 0 {
// In some cases (often with media keys) the device reports a scancode of 0 but a
// valid virtual key. In these cases we obtain the scancode from the virtual key.
unsafe { MapVirtualKeyExW(vkey as u32, MAPVK_VK_TO_VSC_EX, layout.hkl as HKL) as u16 }
} else {
new_ex_scancode(lparam_struct.scancode, lparam_struct.extended)
};
let physical_key = PhysicalKey::from_scancode(scancode as u32);
let location = get_location(scancode, layout.hkl as HKL);
let kbd_state = get_kbd_state();
let mods = WindowsModifiers::active_modifiers(&kbd_state);
let mods_without_ctrl = mods.remove_only_ctrl();
let num_lock_on = kbd_state[VK_NUMLOCK as usize] & 1 != 0;
// On Windows Ctrl+NumLock = Pause (and apparently Ctrl+Pause -> NumLock). In these cases
// the KeyCode still stores the real key, so in the name of consistency across platforms, we
// circumvent this mapping and force the key values to match the keycode.
// For more on this, read the article by Raymond Chen, titled:
// "Why does Ctrl+ScrollLock cancel dialogs?"
// https://devblogs.microsoft.com/oldnewthing/20080211-00/?p=23503
let code_as_key = if mods.contains(WindowsModifiers::CONTROL) {
match physical_key {
PhysicalKey::Code(KeyCode::NumLock) => Some(Key::Named(NamedKey::NumLock)),
PhysicalKey::Code(KeyCode::Pause) => Some(Key::Named(NamedKey::Pause)),
_ => None,
}
} else {
None
};
let preliminary_logical_key =
layout.get_key(mods_without_ctrl, num_lock_on, vkey, &physical_key);
let key_is_char = matches!(preliminary_logical_key, Key::Character(_));
let is_pressed = state == ElementState::Pressed;
let logical_key = if let Some(key) = code_as_key.clone() {
PartialLogicalKey::This(key)
} else if is_pressed && key_is_char && !mods.contains(WindowsModifiers::CONTROL) {
// In some cases we want to use the UNICHAR text for logical_key in order to allow
// dead keys to have an effect on the character reported by `logical_key`.
PartialLogicalKey::TextOr(preliminary_logical_key)
} else {
PartialLogicalKey::This(preliminary_logical_key)
};
let key_without_modifiers = if let Some(key) = code_as_key {
key
} else {
match layout.get_key(NO_MODS, false, vkey, &physical_key) {
// We convert dead keys into their character.
// The reason for this is that `key_without_modifiers` is designed for key-bindings,
// but the US International layout treats `'` (apostrophe) as a dead key and the
// reguar US layout treats it a character. In order for a single binding
// configuration to work with both layouts, we forward each dead key as a character.
Key::Dead(k) => {
if let Some(ch) = k {
// I'm avoiding the heap allocation. I don't want to talk about it :(
let mut utf8 = [0; 4];
let s = ch.encode_utf8(&mut utf8);
Key::Character(SmolStr::new(s))
} else {
Key::Unidentified(NativeKey::Unidentified)
}
}
key => key,
}
};
PartialKeyEventInfo {
vkey,
key_state: state,
logical_key,
key_without_modifiers,
is_repeat: lparam_struct.is_repeat,
physical_key,
location,
utf16parts: Vec::with_capacity(8),
text: PartialText::System(Vec::new()),
}
}
fn finalize(self) -> KeyEvent {
let mut char_with_all_modifiers = None;
if !self.utf16parts.is_empty() {
let os_string = OsString::from_wide(&self.utf16parts);
if let Ok(string) = os_string.into_string() {
char_with_all_modifiers = Some(SmolStr::new(string));
}
}
// The text without Ctrl
let mut text = None;
match self.text {
PartialText::System(wide) => {
if !wide.is_empty() {
let os_string = OsString::from_wide(&wide);
if let Ok(string) = os_string.into_string() {
text = Some(SmolStr::new(string));
}
}
}
PartialText::Text(s) => {
text = s.map(SmolStr::new);
}
}
let logical_key = match self.logical_key {
PartialLogicalKey::TextOr(fallback) => match text.as_ref() {
Some(s) => {
if s.grapheme_indices(true).count() > 1 {
fallback
} else {
Key::Character(s.clone())
}
}
None => Key::Unidentified(NativeKey::Windows(self.vkey)),
},
PartialLogicalKey::This(v) => v,
};
KeyEvent {
physical_key: self.physical_key,
logical_key,
text,
location: self.location,
state: self.key_state,
repeat: self.is_repeat,
platform_specific: KeyEventExtra {
text_with_all_modifers: char_with_all_modifiers,
key_without_modifiers: self.key_without_modifiers,
},
}
}
}
#[derive(Debug, Copy, Clone)]
struct KeyLParam {
pub scancode: u8,
pub extended: bool,
/// This is `previous_state XOR transition_state`. See the lParam for WM_KEYDOWN and WM_KEYUP for further details.
pub is_repeat: bool,
}
fn destructure_key_lparam(lparam: LPARAM) -> KeyLParam {
let previous_state = (lparam >> 30) & 0x01;
let transition_state = (lparam >> 31) & 0x01;
KeyLParam {
scancode: ((lparam >> 16) & 0xFF) as u8,
extended: ((lparam >> 24) & 0x01) != 0,
is_repeat: (previous_state ^ transition_state) != 0,
}
}
#[inline]
fn new_ex_scancode(scancode: u8, extended: bool) -> ExScancode {
(scancode as u16) | (if extended { 0xE000 } else { 0 })
}
#[inline]
fn ex_scancode_from_lparam(lparam: LPARAM) -> ExScancode {
let lparam = destructure_key_lparam(lparam);
new_ex_scancode(lparam.scancode, lparam.extended)
}
/// Gets the keyboard state as reported by messages that have been removed from the event queue.
/// See also: get_async_kbd_state
fn get_kbd_state() -> [u8; 256] {
unsafe {
let mut kbd_state: MaybeUninit<[u8; 256]> = MaybeUninit::uninit();
GetKeyboardState(kbd_state.as_mut_ptr() as *mut u8);
kbd_state.assume_init()
}
}
/// Gets the current keyboard state regardless of whether the corresponding keyboard events have
/// been removed from the event queue. See also: get_kbd_state
#[allow(clippy::uninit_assumed_init)]
fn get_async_kbd_state() -> [u8; 256] {
unsafe {
let mut kbd_state: [u8; 256] = [0; 256];
for (vk, state) in kbd_state.iter_mut().enumerate() {
let vk = vk as VIRTUAL_KEY;
let async_state = GetAsyncKeyState(vk as i32);
let is_down = (async_state & (1 << 15)) != 0;
*state = if is_down { 0x80 } else { 0 };
if matches!(vk, VK_CAPITAL | VK_NUMLOCK | VK_SCROLL) {
// Toggle states aren't reported by `GetAsyncKeyState`
let toggle_state = GetKeyState(vk as i32);
let is_active = (toggle_state & 1) != 0;
*state |= u8::from(is_active);
}
}
kbd_state
}
}
/// On windows, AltGr == Ctrl + Alt
///
/// Due to this equivalence, the system generates a fake Ctrl key-press (and key-release) preceeding
/// every AltGr key-press (and key-release). We check if the current event is a Ctrl event and if
/// the next event is a right Alt (AltGr) event. If this is the case, the current event must be the
/// fake Ctrl event.
fn is_current_fake(curr_info: &PartialKeyEventInfo, next_msg: MSG, layout: &Layout) -> bool {
let curr_is_ctrl = matches!(
curr_info.logical_key,
PartialLogicalKey::This(Key::Named(NamedKey::Control))
);
if layout.has_alt_graph {
let next_code = ex_scancode_from_lparam(next_msg.lParam);
let next_is_altgr = next_code == 0xE038; // 0xE038 is right alt
if curr_is_ctrl && next_is_altgr {
return true;
}
}
false
}
enum PendingMessage<T> {
Incomplete,
Complete(T),
}
struct IdentifiedPendingMessage<T> {
token: PendingMessageToken,
msg: PendingMessage<T>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PendingMessageToken(u32);
/// While processing keyboard events, we sometimes need
/// to call `PeekMessageW` (`next_msg`). But `PeekMessageW`
/// can also call the event handler, which means that the new event
/// gets processed before finishing to process the one that came before.
///
/// This would mean that the application receives events in the wrong order.
/// To avoid this, we keep track whether we are in the middle of processing
/// an event. Such an event is an "incomplete pending event". A
/// "complete pending event" is one that has already finished processing, but
/// hasn't been dispatched to the application because there still are incomplete
/// pending events that came before it.
///
/// When we finish processing an event, we call `complete_pending`,
/// which returns an empty array if there are incomplete pending events, but
/// if all pending events are complete, then it returns all pending events in
/// the order they were encountered. These can then be dispatched to the application
pub struct PendingEventQueue<T> {
pending: Mutex<Vec<IdentifiedPendingMessage<T>>>,
next_id: AtomicU32,
}
impl<T> PendingEventQueue<T> {
/// Add a new pending event to the "pending queue"
pub fn add_pending(&self) -> PendingMessageToken {
let token = self.next_token();
let mut pending = self.pending.lock().unwrap();
pending.push(IdentifiedPendingMessage {
token,
msg: PendingMessage::Incomplete,
});
token
}
/// Returns all finished pending events
///
/// If the return value is non empty, it's guaranteed to contain `msg`
///
/// See also: `add_pending`
pub fn complete_pending(&self, token: PendingMessageToken, msg: T) -> Vec<T> {
let mut pending = self.pending.lock().unwrap();
let mut target_is_first = false;
for (i, pending_msg) in pending.iter_mut().enumerate() {
if pending_msg.token == token {
pending_msg.msg = PendingMessage::Complete(msg);
if i == 0 {
target_is_first = true;
}
break;
}
}
if target_is_first {
// If the message that we just finished was the first one in the pending queue,
// then we can empty the queue, and dispatch all of the messages.
Self::drain_pending(&mut *pending)
} else {
Vec::new()
}
}
pub fn complete_multi(&self, msgs: Vec<T>) -> Vec<T> {
let mut pending = self.pending.lock().unwrap();
if pending.is_empty() {
return msgs;
}
pending.reserve(msgs.len());
for msg in msgs {
pending.push(IdentifiedPendingMessage {
token: self.next_token(),
msg: PendingMessage::Complete(msg),
});
}
Vec::new()
}
/// Returns all finished pending events
///
/// It's safe to call this even if the element isn't in the list anymore
///
/// See also: `add_pending`
pub fn remove_pending(&self, token: PendingMessageToken) -> Vec<T> {
let mut pending = self.pending.lock().unwrap();
let mut was_first = false;
if let Some(m) = pending.first() {
if m.token == token {
was_first = true;
}
}
pending.retain(|m| m.token != token);
if was_first {
Self::drain_pending(&mut *pending)
} else {
Vec::new()
}
}
fn drain_pending(pending: &mut Vec<IdentifiedPendingMessage<T>>) -> Vec<T> {
pending.drain(..).map(|m| {
match m.msg {
PendingMessage::Complete(msg) => msg,
PendingMessage::Incomplete => {
panic!("Found an incomplete pending message when collecting messages. This indicates a bug in winit.")
}
}
}).collect()
}
fn next_token(&self) -> PendingMessageToken {
// It's okay for the u32 to overflow here. Yes, that could mean
// that two different messages have the same token,
// but that would only happen after having about 4 billion
// messages sitting in the pending queue.
//
// In that case, having two identical tokens is the least of your concerns.
let id = self.next_id.fetch_add(1, Relaxed);
PendingMessageToken(id)
}
}
impl<T> Default for PendingEventQueue<T> {
fn default() -> Self {
PendingEventQueue {
pending: Mutex::new(Vec::new()),
next_id: AtomicU32::new(0),
}
}
}
/// WARNING: Due to using PeekMessage, the event handler
/// function may get called during this function.
/// (Re-entrance to the event handler)
///
/// This can cause a deadlock if calling this function
/// while having a mutex locked.
///
/// It can also cause code to get executed in a surprising order.
pub fn next_kbd_msg(hwnd: HWND) -> Option<MSG> {
unsafe {
let mut next_msg = MaybeUninit::uninit();
let peek_retval = PeekMessageW(
next_msg.as_mut_ptr(),
hwnd,
WM_KEYFIRST,
WM_KEYLAST,
PM_NOREMOVE,
);
(peek_retval != 0).then(|| next_msg.assume_init())
}
}
fn get_location(scancode: ExScancode, hkl: HKL) -> KeyLocation {
const ABNT_C2: VIRTUAL_KEY = VK_ABNT_C2 as VIRTUAL_KEY;
let extension = 0xE000;
let extended = (scancode & extension) == extension;
let vkey = unsafe { MapVirtualKeyExW(scancode as u32, MAPVK_VSC_TO_VK_EX, hkl) as VIRTUAL_KEY };
// Use the native VKEY and the extended flag to cover most cases
// This is taken from the `druid` GUI library, specifically
// druid-shell/src/platform/windows/keyboard.rs
match vkey {
VK_LSHIFT | VK_LCONTROL | VK_LMENU | VK_LWIN => KeyLocation::Left,
VK_RSHIFT | VK_RCONTROL | VK_RMENU | VK_RWIN => KeyLocation::Right,
VK_RETURN if extended => KeyLocation::Numpad,
VK_INSERT | VK_DELETE | VK_END | VK_DOWN | VK_NEXT | VK_LEFT | VK_CLEAR | VK_RIGHT
| VK_HOME | VK_UP | VK_PRIOR => {
if extended {
KeyLocation::Standard
} else {
KeyLocation::Numpad
}
}
VK_NUMPAD0 | VK_NUMPAD1 | VK_NUMPAD2 | VK_NUMPAD3 | VK_NUMPAD4 | VK_NUMPAD5
| VK_NUMPAD6 | VK_NUMPAD7 | VK_NUMPAD8 | VK_NUMPAD9 | VK_DECIMAL | VK_DIVIDE
| VK_MULTIPLY | VK_SUBTRACT | VK_ADD | ABNT_C2 => KeyLocation::Numpad,
_ => KeyLocation::Standard,
}
}
impl PhysicalKeyExtScancode for PhysicalKey {
fn to_scancode(self) -> Option<u32> {
// See `from_scancode` for more info
let hkl = unsafe { GetKeyboardLayout(0) };
let primary_lang_id = primarylangid(loword(hkl as u32));
let is_korean = primary_lang_id as u32 == LANG_KOREAN;
let code = match self {
PhysicalKey::Code(code) => code,
PhysicalKey::Unidentified(code) => {
return match code {
NativeKeyCode::Windows(scancode) => Some(scancode as u32),
_ => None,
};
}
};
match code {
KeyCode::Backquote => Some(0x0029),
KeyCode::Backslash => Some(0x002B),
KeyCode::Backspace => Some(0x000E),
KeyCode::BracketLeft => Some(0x001A),
KeyCode::BracketRight => Some(0x001B),
KeyCode::Comma => Some(0x0033),
KeyCode::Digit0 => Some(0x000B),
KeyCode::Digit1 => Some(0x0002),
KeyCode::Digit2 => Some(0x0003),
KeyCode::Digit3 => Some(0x0004),
KeyCode::Digit4 => Some(0x0005),
KeyCode::Digit5 => Some(0x0006),
KeyCode::Digit6 => Some(0x0007),
KeyCode::Digit7 => Some(0x0008),
KeyCode::Digit8 => Some(0x0009),
KeyCode::Digit9 => Some(0x000A),
KeyCode::Equal => Some(0x000D),
KeyCode::IntlBackslash => Some(0x0056),
KeyCode::IntlRo => Some(0x0073),
KeyCode::IntlYen => Some(0x007D),
KeyCode::KeyA => Some(0x001E),
KeyCode::KeyB => Some(0x0030),
KeyCode::KeyC => Some(0x002E),
KeyCode::KeyD => Some(0x0020),
KeyCode::KeyE => Some(0x0012),
KeyCode::KeyF => Some(0x0021),
KeyCode::KeyG => Some(0x0022),
KeyCode::KeyH => Some(0x0023),
KeyCode::KeyI => Some(0x0017),
KeyCode::KeyJ => Some(0x0024),
KeyCode::KeyK => Some(0x0025),
KeyCode::KeyL => Some(0x0026),
KeyCode::KeyM => Some(0x0032),
KeyCode::KeyN => Some(0x0031),
KeyCode::KeyO => Some(0x0018),
KeyCode::KeyP => Some(0x0019),