From d85b7bf3b9708d229fe1d7639d6bfb504177481e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 8 Jun 2022 07:38:02 -0700 Subject: [PATCH] win32: add extended/enhanced key concept for win32 input mode refs: https://github.com/wez/wezterm/issues/2009 refs: https://github.com/microsoft/terminal/issues/13134#issuecomment-1148000328 --- wezterm-gui/src/inputmap.rs | 15 +++------------ wezterm-input-types/src/lib.rs | 22 ++++++++++++++++++++++ window/src/os/windows/window.rs | 1 + 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/wezterm-gui/src/inputmap.rs b/wezterm-gui/src/inputmap.rs index 1ad6b612c..9775c2bce 100644 --- a/wezterm-gui/src/inputmap.rs +++ b/wezterm-gui/src/inputmap.rs @@ -232,22 +232,13 @@ impl InputMap { pub fn is_leader(&self, key: &KeyCode, mods: Modifiers) -> Option { if let Some((leader_key, leader_mods, timeout)) = self.leader.as_ref() { - if *leader_key == *key && *leader_mods == Self::remove_positional_mods(mods) { + if *leader_key == *key && *leader_mods == mods.remove_positional_mods() { return Some(timeout.clone()); } } None } - fn remove_positional_mods(mods: Modifiers) -> Modifiers { - mods - (Modifiers::LEFT_ALT - | Modifiers::RIGHT_ALT - | Modifiers::LEFT_CTRL - | Modifiers::RIGHT_CTRL - | Modifiers::LEFT_SHIFT - | Modifiers::RIGHT_SHIFT) - } - pub fn has_table(&self, name: &str) -> bool { self.keys.by_name.contains_key(name) } @@ -264,13 +255,13 @@ impl InputMap { }; table - .get(&key.normalize_shift(Self::remove_positional_mods(mods))) + .get(&key.normalize_shift(mods.remove_positional_mods())) .cloned() } pub fn lookup_mouse(&self, event: MouseEventTrigger, mods: Modifiers) -> Option { self.mouse - .get(&(event, Self::remove_positional_mods(mods))) + .get(&(event, mods.remove_positional_mods())) .cloned() } } diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 54d36bf92..906f23096 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -466,6 +466,7 @@ bitflags! { const RIGHT_CTRL = 1<<9; const LEFT_SHIFT = 1<<10; const RIGHT_SHIFT = 1<<11; + const ENHANCED_KEY = 1<<12; } } @@ -524,6 +525,7 @@ impl ToString for Modifiers { (Self::RIGHT_CTRL, "RIGHT_CTRL"), (Self::LEFT_SHIFT, "LEFT_SHIFT"), (Self::RIGHT_SHIFT, "RIGHT_SHIFT"), + (Self::ENHANCED_KEY, "ENHANCED_KEY"), ] { if !self.contains(value) { continue; @@ -538,6 +540,22 @@ impl ToString for Modifiers { } } +impl Modifiers { + /// Remove positional and other "supplemental" bits that + /// are used to carry around implementation details, but that + /// are not bits that should be matched when matching key + /// assignments. + pub fn remove_positional_mods(self) -> Self { + self - (Self::LEFT_ALT + | Self::RIGHT_ALT + | Self::LEFT_CTRL + | Self::RIGHT_CTRL + | Self::LEFT_SHIFT + | Self::RIGHT_SHIFT + | Self::ENHANCED_KEY) + } +} + /// These keycodes identify keys based on their physical /// position on an ANSI-standard US keyboard. #[derive( @@ -1169,6 +1187,7 @@ impl KeyEvent { // defines the dwControlKeyState values let mut control_key_state = 0; const SHIFT_PRESSED: usize = 0x10; + const ENHANCED_KEY: usize = 0x100; const RIGHT_ALT_PRESSED: usize = 0x01; const LEFT_ALT_PRESSED: usize = 0x02; const LEFT_CTRL_PRESSED: usize = 0x08; @@ -1194,6 +1213,9 @@ impl KeyEvent { if self.modifiers.contains(Modifiers::LEFT_CTRL) { control_key_state |= LEFT_CTRL_PRESSED; } + if self.modifiers.contains(Modifiers::ENHANCED_KEY) { + control_key_state |= ENHANCED_KEY; + } let key_down = if self.key_is_down { 1 } else { 0 }; diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 8833ca075..e8c6fbc0c 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -2216,6 +2216,7 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option