From 2803e8fc58bd6adad2d549db11f4d3224dddc675 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 13 Jun 2020 22:40:53 -0700 Subject: [PATCH] wezterm: improve left vs right alt behavior on macos This commit allows distinguishing between left and right alt modifiers at the window layer. So far only macos provides this additional information. Expand the logic that decides whether Alt should emit the composed key or act as the raw key with the Alt modifier flag set so that we can set that behavior separately for the left and right modifiers on systems that support it, and use the existing config for systems that don't support it. The default settings for these flags is that Left Alt will send the uncomposed key + Alt modifier while the Right Alt will behave more like AltGr (which is typically on the RHS of the keyboard) and send the composed key. This gives more flexibility by default and hopefully matches expectations a bit better. refs: https://github.com/wez/wezterm/issues/216 --- docs/changelog.markdown | 12 ++++++++++ src/config/mod.rs | 6 +++++ src/frontend/gui/termwindow.rs | 43 +++++++++++++++++++++++++--------- window/src/input.rs | 2 ++ window/src/os/macos/window.rs | 7 ++++-- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index 0c6310efe..56649e04b 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -22,6 +22,18 @@ brief notes about them may accumulate here. the box experience for most users. * macos: we now attempt to set a reasonable default LANG environment based on the locale settings at the time that wezterm is launched. +* macos: introduce `send_composed_key_when_left_alt_is_pressed` and + `send_composed_key_when_right_alt_is_pressed` boolean config settings. Like + the existing `send_composed_key_when_alt_is_pressed` option, these control + whether the `Alt` or `Option` modifier produce composed output or generate + the raw key position with the ALT modifier applied. The difference from the + existing config option is that on systems where Left and Right Alt can be + distinguished you now have the ability to control this behavior + independently. The default behavior on these systems is + `send_composed_key_when_left_alt_is_pressed=false` and + `send_composed_key_when_right_alt_is_pressed=true` so that the right Alt key + behaves more like an `AltGr` key and generates the composed input, while the + Left Alt is regular uncomposed Alt. ### 20200607-144723-74889cd4 diff --git a/src/config/mod.rs b/src/config/mod.rs index d6a113586..5383821c7 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -436,6 +436,12 @@ pub struct Config { #[serde(default = "default_true")] pub send_composed_key_when_alt_is_pressed: bool, + #[serde(default)] + pub send_composed_key_when_left_alt_is_pressed: bool, + + #[serde(default = "default_true")] + pub send_composed_key_when_right_alt_is_pressed: bool, + /// If true, the `Backspace` and `Delete` keys generate `Delete` and `Backspace` /// keypresses, respectively, rather than their normal keycodes. /// On macOS the default for this is true because its Backspace key diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index b04e1f5d8..bf7e7c505 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -443,8 +443,8 @@ impl WindowCallbacks for TermWindow { self.scaling_changed(dimensions, self.fonts.get_font_scale()); } - fn key_event(&mut self, key: &KeyEvent, context: &dyn WindowOps) -> bool { - if !key.key_is_down { + fn key_event(&mut self, window_key: &KeyEvent, context: &dyn WindowOps) -> bool { + if !window_key.key_is_down { return false; } @@ -454,12 +454,12 @@ impl WindowCallbacks for TermWindow { Some(tab) => tab, None => return false, }; - let modifiers = window_mods_to_termwiz_mods(key.modifiers); - let raw_modifiers = window_mods_to_termwiz_mods(key.raw_modifiers); + let modifiers = window_mods_to_termwiz_mods(window_key.modifiers); + let raw_modifiers = window_mods_to_termwiz_mods(window_key.raw_modifiers); // First chance to operate on the raw key; if it matches a // user-defined key binding then we execute it and stop there. - if let Some(key) = &key.raw_key { + if let Some(key) = &window_key.raw_key { if let Key::Code(key) = self.win_key_code_to_termwiz_key_code(&key) { if let Some(assignment) = self.input_map.lookup_key(key, raw_modifiers) { self.perform_key_assignment(&tab, &assignment).ok(); @@ -467,10 +467,28 @@ impl WindowCallbacks for TermWindow { return true; } - if !configuration().send_composed_key_when_alt_is_pressed - && raw_modifiers.contains(::termwiz::input::Modifiers::ALT) - && tab.key_down(key, raw_modifiers).is_ok() - { + let config = configuration(); + + // This is a bit ugly. + // Not all of our platforms report LEFT|RIGHT ALT; most report just ALT. + // For those that do distinguish between them we want to respect the left vs. + // right settings for the compose behavior. + // Otherwise, if the event didn't include left vs. right then we want to + // respect the generic compose behavior. + let bypass_compose = + // Left ALT and they disabled compose + (window_key.raw_modifiers.contains(Modifiers::LEFT_ALT) + && !config.send_composed_key_when_left_alt_is_pressed) + // Right ALT and they disabled compose + || (window_key.raw_modifiers.contains(Modifiers::RIGHT_ALT) + && !config.send_composed_key_when_right_alt_is_pressed) + // Generic ALT and they disabled generic compose + || (!window_key.raw_modifiers.contains(Modifiers::RIGHT_ALT) + && !window_key.raw_modifiers.contains(Modifiers::LEFT_ALT) + && window_key.raw_modifiers.contains(Modifiers::ALT) + && !config.send_composed_key_when_alt_is_pressed); + + if bypass_compose && tab.key_down(key, raw_modifiers).is_ok() { if !key.is_modifier() && self.tab_state(tab.tab_id()).overlay.is_none() { self.maybe_scroll_to_bottom_for_input(&tab); } @@ -480,7 +498,7 @@ impl WindowCallbacks for TermWindow { } } - let key = self.win_key_code_to_termwiz_key_code(&key.key); + let key = self.win_key_code_to_termwiz_key_code(&window_key.key); match key { Key::Code(key) => { if let Some(assignment) = self.input_map.lookup_key(key, modifiers) { @@ -3245,7 +3263,10 @@ fn window_mods_to_termwiz_mods(modifiers: ::window::Modifiers) -> termwiz::input if modifiers.contains(::window::Modifiers::SHIFT) { result.insert(termwiz::input::Modifiers::SHIFT); } - if modifiers.contains(::window::Modifiers::ALT) { + if modifiers.contains(::window::Modifiers::LEFT_ALT) { + result.insert(termwiz::input::Modifiers::ALT); + } + if modifiers.contains(::window::Modifiers::RIGHT_ALT) { result.insert(termwiz::input::Modifiers::ALT); } if modifiers.contains(::window::Modifiers::CTRL) { diff --git a/window/src/input.rs b/window/src/input.rs index 24fd48aed..e21bccfc5 100644 --- a/window/src/input.rs +++ b/window/src/input.rs @@ -91,6 +91,8 @@ bitflags! { const ALT = 1<<2; const CTRL = 1<<3; const SUPER = 1<<4; + const LEFT_ALT = 1<<5; + const RIGHT_ALT = 1<<6; } } bitflags! { diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 09fc820a2..633a5fa38 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -711,8 +711,11 @@ fn key_modifiers(flags: NSEventModifierFlags) -> Modifiers { if flags.contains(NSEventModifierFlags::NSShiftKeyMask) { mods |= Modifiers::SHIFT; } - if flags.contains(NSEventModifierFlags::NSAlternateKeyMask) { - mods |= Modifiers::ALT; + if flags.contains(NSEventModifierFlags::NSAlternateKeyMask) && (flags.bits() & 0x20) != 0 { + mods |= Modifiers::LEFT_ALT | Modifiers::ALT; + } + if flags.contains(NSEventModifierFlags::NSAlternateKeyMask) && (flags.bits() & 0x40) != 0 { + mods |= Modifiers::RIGHT_ALT | Modifiers::ALT; } if flags.contains(NSEventModifierFlags::NSControlKeyMask) { mods |= Modifiers::CTRL;