@@ -90,23 +90,20 @@ impl WinVt {
9090}
9191
9292fn decode_key_record < F : FnMut ( InternalTermEvent ) > (
93- input_parser : & mut InputParser ,
9493 record : & KEY_EVENT_RECORD ,
9594 f : & mut F ,
9695) {
9796 use winapi:: um:: winuser:: * ;
9897
98+ let uchar = unsafe { * record. uChar . UnicodeChar ( ) } ;
99+
99100 let modifiers = modifiers_from_ctrl_key_state ( record. dwControlKeyState ) ;
100101 let virtual_key_code = record. wVirtualKeyCode as i32 ;
101102
102103 // We normally ignore all key release events, but we will make an exception for an Alt key
103104 // release if it carries a u_char value, as this indicates an Alt code.
104- let is_alt_code = virtual_key_code == VK_MENU
105- && record. bKeyDown == 0
106- && * unsafe { record. uChar . UnicodeChar ( ) } != 0 ;
107- if is_alt_code {
108- let utf16 = * unsafe { record. uChar . UnicodeChar ( ) } ;
109- match utf16 {
105+ if is_alt_code ( record) {
106+ match uchar {
110107 surrogate @ 0xD800 ..=0xDFFF => {
111108 log:: error!( "Unhandled surrogate key record." ) ;
112109 return ;
@@ -157,8 +154,7 @@ fn decode_key_record<F: FnMut(InternalTermEvent)>(
157154 VK_TAB if modifiers. contains ( KeyModifiers :: SHIFT ) => Some ( KeyCode :: BackTab ) ,
158155 VK_TAB => Some ( KeyCode :: Tab ) ,
159156 _ => {
160- let utf16 = * unsafe { record. uChar . UnicodeChar ( ) } ;
161- match utf16 {
157+ match uchar {
162158 0x00 ..=0x1f => {
163159 // Some key combinations generate either no u_char value or generate control
164160 // codes. To deliver back a KeyCode::Char(...) event we want to know which
@@ -193,6 +189,12 @@ fn decode_key_record<F: FnMut(InternalTermEvent)>(
193189 }
194190}
195191
192+ fn is_alt_code ( record : & KEY_EVENT_RECORD ) -> bool {
193+ record. wVirtualKeyCode as i32 == winapi:: um:: winuser:: VK_MENU
194+ && record. bKeyDown == 0
195+ && unsafe { * record. uChar . UnicodeChar ( ) } != 0
196+ }
197+
196198enum CharCase {
197199 LowerCase ,
198200 UpperCase ,
@@ -398,7 +400,30 @@ pub fn decode_input_records<F: FnMut(InternalTermEvent)>(
398400 for record in records {
399401 match record. EventType {
400402 KEY_EVENT => {
401- decode_key_record ( input_parser, unsafe { record. Event . KeyEvent ( ) } , f)
403+ let record = unsafe { record. Event . KeyEvent ( ) } ;
404+ let uchar = unsafe { * record. uChar . UnicodeChar ( ) } ;
405+
406+ if record. bKeyDown == 0 && !is_alt_code ( record) {
407+ // Ignore release events.
408+ //
409+ // If we want to support release key events on Windows we need to
410+ // handle:
411+ // - if the key is part of escape sequence: consume both press+release
412+ // events
413+ // - otherwise emit both press and release
414+ } else if ( 1 ..=0x7f ) . contains ( & uchar)
415+ && record. dwControlKeyState == 0
416+ && record. bKeyDown == 1
417+ {
418+ let ch = uchar as u8 as char ;
419+ let mut buf = [ 0u8 ; 4 ] ;
420+ let bytes = ch. encode_utf8 ( & mut buf) . as_bytes ( ) ;
421+ for _ in 0 ..record. wRepeatCount {
422+ input_parser. parse_input ( bytes, true , true , & mut * f) ;
423+ }
424+ } else {
425+ decode_key_record ( record, f) ;
426+ }
402427 }
403428 MOUSE_EVENT => decode_mouse_record (
404429 input_parser,
@@ -411,6 +436,9 @@ pub fn decode_input_records<F: FnMut(InternalTermEvent)>(
411436 _ => { }
412437 }
413438 }
439+
440+ // Process pending ESC character.
441+ input_parser. parse_input ( b"" , true , false , f) ;
414442}
415443
416444fn modifiers_from_ctrl_key_state ( state : u32 ) -> KeyModifiers {
0 commit comments