From 7ddff705a422dcc9b0a607d1fabbe08aeddbc24a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 20 May 2020 20:32:03 -0700 Subject: [PATCH] window: Windows: improve AltGr handling Adds some detection to see if the active keyboard layout has AltGr, and if so, adjust our key mapping logic to accomodate it. With this change, when using an ENG layout, I can use either left or right alt-b/alt-f to move through words in wsl. When I switch to DEU my left alt is still alt and my right alt causes the Windows On-Screen keyboard to act as though AltGr is pressed. I can then use the On-Screen keyboard to press the `<` key which is to the left of the `Z` key on a German layout and have it produce the `|` character. refs: https://github.com/wez/wezterm/issues/185 --- window/src/os/windows/window.rs | 108 +++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 9 deletions(-) diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index e3106254b..cd7a09d71 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -47,6 +47,8 @@ pub(crate) struct WindowInner { /// Fraction of mouse scroll hscroll_remainder: i16, vscroll_remainder: i16, + + keyboard_info: KeyboardLayoutInfo, } #[derive(Debug, Clone)] @@ -190,6 +192,7 @@ impl Window { gl_state: None, vscroll_remainder: 0, hscroll_remainder: 0, + keyboard_info: KeyboardLayoutInfo::new(), })); // Careful: `raw` owns a ref to inner, but there is no Drop impl @@ -930,9 +933,79 @@ unsafe fn ime_composition( None } +/// Holds information about the current keyboard layout. +/// This is used to determine whether the layout includes +/// an AltGr key or just has a regular Right-Alt key. +struct KeyboardLayoutInfo { + layout: HKL, + has_alt_gr: bool, +} + +impl KeyboardLayoutInfo { + pub fn new() -> Self { + Self { + layout: std::ptr::null_mut(), + has_alt_gr: false, + } + } + + /// Probe to detect whether an AltGr key is present. + /// This is done by synthesizing a keyboard state with control and alt + /// pressed and then testing the virtual key presses. If we find that + /// one of these yields a single unicode character output then we assume that + /// it does have AltGr. + unsafe fn update(&mut self) { + let current_layout = GetKeyboardLayout(0); + if current_layout == self.layout { + // Avoid recomputing this if the layout hasn't changed + return; + } + + let mut saved_state = [0u8; 256]; + if GetKeyboardState(saved_state.as_mut_ptr()) == 0 { + return; + } + + self.has_alt_gr = false; + + let mut state = [0u8; 256]; + state[VK_CONTROL as usize] = 0x80; + state[VK_MENU as usize] = 0x80; + + for vk in 0..=255u32 { + if vk == VK_PACKET as _ { + // Avoid false positives + continue; + } + + let mut out = [0u16; 16]; + let ret = ToUnicode(vk, 0, state.as_ptr(), out.as_mut_ptr(), out.len() as i32, 0); + if ret == 1 { + self.has_alt_gr = true; + break; + } + + if ret == -1 { + // Dead key; keep clocking the state to clear out its effects + while ToUnicode(vk, 0, state.as_ptr(), out.as_mut_ptr(), out.len() as i32, 0) < 0 {} + } + } + + SetKeyboardState(saved_state.as_mut_ptr()); + self.layout = current_layout; + } + + pub fn has_alt_gr(&mut self) -> bool { + unsafe { + self.update(); + } + self.has_alt_gr + } +} + unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option { if let Some(inner) = rc_from_hwnd(hwnd) { - let inner = inner.borrow(); + let mut inner = inner.borrow_mut(); let repeat = (lparam & 0xffff) as u16; let scan_code = ((lparam >> 16) & 0xff) as u8; let releasing = (lparam & (1 << 31)) != 0; @@ -940,6 +1013,7 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option "WM_CHAR", @@ -951,8 +1025,8 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option "WAT", }; eprintln!( - "{} c=`{}` repeat={} scan={} alt_pressed={} was_down={} releasing={} IME={}", - label, wparam, repeat, scan_code, alt_pressed, was_down, releasing, ime_active + "{} c=`{}` repeat={} scan={} is_extended={} alt_pressed={} was_down={} releasing={} IME={}", + label, wparam, repeat, scan_code, is_extended, alt_pressed, was_down, releasing, ime_active ); */ @@ -967,14 +1041,26 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option Option None, 0 => { + /* + let mapped_vkey = MapVirtualKeyW(scan_code.into(), MAPVK_VSC_TO_VK_EX) as i32; + eprintln!("mapped vkey={} vs wparam vkey {}", mapped_vkey, wparam); + */ // No unicode translation, so map the scan code to a virtual key // code, and from there map it to our KeyCode type - match MapVirtualKeyW(scan_code.into(), MAPVK_VSC_TO_VK_EX) as i32 { + match wparam as i32 { 0 => None, VK_CANCEL => Some(KeyCode::Cancel), VK_BACK => Some(KeyCode::Char('\u{8}')),