From 2dcf7ad98dde802bb92cbf78a98662ea7224842a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 9 Apr 2023 10:20:11 -0700 Subject: [PATCH] keyboard: plumb more modifier+state information through This commit teaches the termwiz layer about positional modifiers, and expands our modifier concept to also pass through led states such as caps lock and num lock. Those aren't actually keyboard modifiers, but the state is useful to recognize. Adjust the shift key normalization so that we don't uppercase alpha characters when both SHIFT and CAPS_LOCK are held. This processing will remove both SHIFT and CAPS_LOCK in that situation. Add a method to KeyEvent that will undo the OS keyboard layer normalization of positional to generic modifier key presses. eg: the OS may map LeftControl -> Control, but we actually prefer to have LeftControl so if we can unambiguously reverse that mapping, we do so. refs: #3476 refs: #3475 --- termwiz/src/input.rs | 62 ++++++++++++++- wezterm-gui/src/termwindow/keyevent.rs | 41 ++++++++-- wezterm-input-types/src/lib.rs | 105 ++++++++++++++++++++++++- window/src/os/macos/window.rs | 3 +- window/src/os/windows/window.rs | 1 + window/src/os/x11/keyboard.rs | 12 ++- window/src/os/x11/window.rs | 3 +- 7 files changed, 214 insertions(+), 13 deletions(-) diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index 4eb74673e..1ad1dc1f4 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -45,15 +45,25 @@ use winapi::um::wincon::{ bitflags! { #[cfg_attr(feature="use_serde", derive(Serialize, Deserialize))] #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] - pub struct Modifiers: u8 { + pub struct Modifiers: u16 { const NONE = 0; const SHIFT = 1<<1; const ALT = 1<<2; const CTRL = 1<<3; const SUPER = 1<<4; + const LEFT_ALT = 1<<5; + const RIGHT_ALT = 1<<6; /// This is a virtual modifier used by wezterm - #[doc(hidden)] - const LEADER = 1<<5; + const LEADER = 1<<7; + const LEFT_CTRL = 1<<8; + const RIGHT_CTRL = 1<<9; + const LEFT_SHIFT = 1<<10; + const RIGHT_SHIFT = 1<<11; + const ENHANCED_KEY = 1<<12; + /// Not really a modifier, but a keyboard driver state + const CAPS_LOCK = 1<<13; + /// Not really a modifier, but a keyboard driver state + const NUM_LOCK = 1<<14; } } bitflags! { @@ -367,6 +377,12 @@ impl KeyCode { if mods.contains(Modifiers::SUPER) { modifiers |= 8; } + if mods.contains(Modifiers::CAPS_LOCK) { + modifiers |= 64; + } + if mods.contains(Modifiers::NUM_LOCK) { + modifiers |= 128; + } modifiers += 1; let event_type = if flags.contains(KittyKeyboardFlags::REPORT_EVENT_TYPES) && !is_down { @@ -2190,4 +2206,44 @@ mod test { "\u{1b}[97:65:97;1:3u".to_string() ); } + + #[test] + fn encode_issue_3476() { + let mode = KeyCodeEncodeModes { + encoding: KeyboardEncoding::Kitty( + KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES + | KittyKeyboardFlags::REPORT_EVENT_TYPES + | KittyKeyboardFlags::REPORT_ALTERNATE_KEYS + | KittyKeyboardFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES, + ), + newline_mode: false, + application_cursor_keys: false, + modify_other_keys: None, + }; + + assert_eq!( + KeyCode::LeftShift + .encode(Modifiers::NONE, mode, true) + .unwrap(), + "\u{1b}[57441;1u".to_string() + ); + assert_eq!( + KeyCode::LeftShift + .encode(Modifiers::NONE, mode, false) + .unwrap(), + "\u{1b}[57441;1:3u".to_string() + ); + assert_eq!( + KeyCode::LeftControl + .encode(Modifiers::NONE, mode, true) + .unwrap(), + "\u{1b}[57442;1u".to_string() + ); + assert_eq!( + KeyCode::LeftControl + .encode(Modifiers::NONE, mode, false) + .unwrap(), + "\u{1b}[57442;1:3u".to_string() + ); + } } diff --git a/wezterm-gui/src/termwindow/keyevent.rs b/wezterm-gui/src/termwindow/keyevent.rs index f5f47c2e7..eda40c390 100644 --- a/wezterm-gui/src/termwindow/keyevent.rs +++ b/wezterm-gui/src/termwindow/keyevent.rs @@ -174,33 +174,62 @@ impl KeyTableState { pub fn window_mods_to_termwiz_mods(modifiers: ::window::Modifiers) -> termwiz::input::Modifiers { let mut result = termwiz::input::Modifiers::NONE; + if modifiers.contains(::window::Modifiers::SHIFT) { result.insert(termwiz::input::Modifiers::SHIFT); } + if modifiers.contains(::window::Modifiers::LEFT_SHIFT) { + result.insert(termwiz::input::Modifiers::LEFT_SHIFT); + } + if modifiers.contains(::window::Modifiers::RIGHT_SHIFT) { + result.insert(termwiz::input::Modifiers::RIGHT_SHIFT); + } + if modifiers.contains(::window::Modifiers::LEFT_ALT) { result.insert(termwiz::input::Modifiers::ALT); + result.insert(termwiz::input::Modifiers::LEFT_ALT); } - /* We DONT want to do this: we carry through RIGHT_ALT - * only for win32-input mode to track when AltGr was used, - * but we don't want that to be treated as regular ALT - * when encoding regular input for the terminal. - * if modifiers.contains(::window::Modifiers::RIGHT_ALT) { + /* We DONT want to do this: we carry through RIGHT_ALT + * only for win32-input mode to track when AltGr was used, + * but we don't want that to be treated as regular ALT + * when encoding regular input for the terminal. + * result.insert(termwiz::input::Modifiers::ALT); + */ + + // But we do want the positional mod + result.insert(termwiz::input::Modifiers::RIGHT_ALT); } - */ if modifiers.contains(::window::Modifiers::ALT) { result.insert(termwiz::input::Modifiers::ALT); } + if modifiers.contains(::window::Modifiers::CTRL) { result.insert(termwiz::input::Modifiers::CTRL); } + if modifiers.contains(::window::Modifiers::LEFT_CTRL) { + result.insert(termwiz::input::Modifiers::LEFT_CTRL); + } + if modifiers.contains(::window::Modifiers::RIGHT_CTRL) { + result.insert(termwiz::input::Modifiers::RIGHT_CTRL); + } + if modifiers.contains(::window::Modifiers::SUPER) { result.insert(termwiz::input::Modifiers::SUPER); } if modifiers.contains(::window::Modifiers::LEADER) { result.insert(termwiz::input::Modifiers::LEADER); } + if modifiers.contains(::window::Modifiers::ENHANCED_KEY) { + result.insert(termwiz::input::Modifiers::ENHANCED_KEY); + } + if modifiers.contains(::window::Modifiers::CAPS_LOCK) { + result.insert(termwiz::input::Modifiers::CAPS_LOCK); + } + if modifiers.contains(::window::Modifiers::NUM_LOCK) { + result.insert(termwiz::input::Modifiers::NUM_LOCK); + } result } diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 655ee7f51..896288f49 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -461,12 +461,17 @@ bitflags! { const SUPER = 1<<4; const LEFT_ALT = 1<<5; const RIGHT_ALT = 1<<6; + /// This is a virtual modifier used by wezterm const LEADER = 1<<7; const LEFT_CTRL = 1<<8; const RIGHT_CTRL = 1<<9; const LEFT_SHIFT = 1<<10; const RIGHT_SHIFT = 1<<11; const ENHANCED_KEY = 1<<12; + /// Not really a modifier, but a keyboard driver state + const CAPS_LOCK = 1<<13; + /// Not really a modifier, but a keyboard driver state + const NUM_LOCK = 1<<14; } } @@ -619,6 +624,24 @@ impl Modifiers { "ENHANCED_KEY", "ENHANCED_KEY", ), + ( + Self::CAPS_LOCK, + "CAPS_LOCK", + "CAPS_LOCK", + "CAPS_LOCK", + "CAPS_LOCK", + "CAPS_LOCK", + "CAPS_LOCK", + ), + ( + Self::NUM_LOCK, + "NUM_LOCK", + "NUM_LOCK", + "NUM_LOCK", + "NUM_LOCK", + "NUM_LOCK", + "NUM_LOCK", + ), ] { if !self.contains(value) { continue; @@ -1223,7 +1246,9 @@ pub struct KeyEvent { } fn normalize_shift(key: KeyCode, modifiers: Modifiers) -> (KeyCode, Modifiers) { - if modifiers.contains(Modifiers::SHIFT) { + if modifiers.contains(Modifiers::SHIFT | Modifiers::CAPS_LOCK) { + (key, modifiers - (Modifiers::SHIFT | Modifiers::CAPS_LOCK)) + } else if modifiers.contains(Modifiers::SHIFT) { match key { KeyCode::Char(c) if c.is_ascii_uppercase() => (key, modifiers - Modifiers::SHIFT), KeyCode::Char(c) if c.is_ascii_lowercase() => ( @@ -1270,6 +1295,84 @@ impl KeyEvent { self } + /// If the key code is a modifier key (Control, Alt, Shift), check + /// the underlying raw event to see if we had a positional version + /// of that key. + /// If so, switch to the positional version. + pub fn resurface_positional_modifier_key(mut self) -> Self { + match self.key { + KeyCode::Control + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::LeftControl | KeyCode::Physical(PhysKeyCode::LeftControl), + .. + }) + ) => + { + self.key = KeyCode::LeftControl; + } + KeyCode::Control + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::RightControl | KeyCode::Physical(PhysKeyCode::RightControl), + .. + }) + ) => + { + self.key = KeyCode::RightControl; + } + KeyCode::Alt + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::LeftAlt | KeyCode::Physical(PhysKeyCode::LeftAlt), + .. + }) + ) => + { + self.key = KeyCode::LeftAlt; + } + KeyCode::Alt + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::RightAlt | KeyCode::Physical(PhysKeyCode::RightAlt), + .. + }) + ) => + { + self.key = KeyCode::RightAlt; + } + KeyCode::Shift + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::LeftShift | KeyCode::Physical(PhysKeyCode::LeftShift), + .. + }) + ) => + { + self.key = KeyCode::LeftShift; + } + KeyCode::Shift + if matches!( + self.raw, + Some(RawKeyEvent { + key: KeyCode::RightShift | KeyCode::Physical(PhysKeyCode::RightShift), + .. + }) + ) => + { + self.key = KeyCode::RightShift; + } + _ => {} + } + + self + } + /// If CTRL is held down and we have KeyCode::Char(_) with the /// ASCII control value encoded, decode it back to the ASCII /// alpha keycode instead. diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 2a51bf809..a926e7e0b 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -2598,7 +2598,8 @@ impl WindowView { key_is_down, raw: Some(raw_key_event), } - .normalize_shift(); + .normalize_shift() + .resurface_positional_modifier_key(); log::debug!( "key_common {:?} (chars={:?} unmod={:?} modifiers={:?})", diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 6bb6f88fc..1e6111401 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -2495,6 +2495,7 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option bool { + self.state.borrow().led_name_is_active(led) + } pub fn get_key_modifiers(&self) -> Modifiers { let mut res = Modifiers::default(); @@ -405,6 +409,12 @@ impl Keyboard { // Mod4 res |= Modifiers::SUPER; } + if self.led_is_active(xkb::LED_NAME_NUM) { + res |= Modifiers::NUM_LOCK; + } + if self.led_is_active(xkb::LED_NAME_CAPS) { + res |= Modifiers::CAPS_LOCK; + } res } diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index dc44d2378..33ba4ab6b 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -638,7 +638,8 @@ impl XWindowInner { key_is_down: true, raw: None, } - .normalize_shift(); + .normalize_shift() + .resurface_positional_modifier_key(); self.events.dispatch(WindowEvent::KeyEvent(key_event)); }