From 94591fb3642c0a3e625a0e24cd6a9892215c6594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20N=C3=BCtzi?= Date: Thu, 7 Dec 2023 18:17:40 +0100 Subject: [PATCH] fix: Emit correct codes for latched keys... - Caps and NumLock should also be correctly encoded if the they are latched. This was ensured by checking the led status. Continue doing this. --- wezterm-input-types/src/lib.rs | 52 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index a1ae14c7f99..635d8e57161 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1685,10 +1685,14 @@ impl KeyEvent { if raw_modifiers.contains(Modifiers::META) { modifiers |= 32; } - if raw_modifiers.contains(Modifiers::CAPS_LOCK) { + if raw_modifiers.contains(Modifiers::CAPS_LOCK) + || self.leds.contains(KeyboardLedStatus::CAPS_LOCK) + { modifiers |= 64; } - if raw_modifiers.contains(Modifiers::NUM_LOCK) { + if raw_modifiers.contains(Modifiers::NUM_LOCK) + || self.leds.contains(KeyboardLedStatus::NUM_LOCK) + { modifiers |= 128; } modifiers += 1; @@ -1757,7 +1761,11 @@ impl KeyEvent { }); if let Some(numpad) = is_numpad { - let code = match (numpad, self.leds.contains(KeyboardLedStatus::NUM_LOCK)) { + let code = match ( + numpad, + self.leds.contains(KeyboardLedStatus::NUM_LOCK) + || raw_modifiers.contains(Modifiers::NUM_LOCK), + ) { (PhysKeyCode::Keypad0, true) => 57399, (PhysKeyCode::Keypad0, false) => 57425, (PhysKeyCode::Keypad1, true) => 57400, @@ -1822,26 +1830,25 @@ impl KeyEvent { && event_type.is_empty() && is_legacy_key && !(flags.contains(KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES) - && (self.modifiers.contains(Modifiers::CTRL) - || self.modifiers.contains(Modifiers::ALT))) - && !self.modifiers.intersects( - Modifiers::SUPER, /* TODO: Hyper and Meta should be added here. */ + && (raw_modifiers.contains(Modifiers::CTRL) + || raw_modifiers.contains(Modifiers::ALT))) + && !raw_modifiers.intersects( + Modifiers::SUPER + | Modifiers::META + | Modifiers::NUM_LOCK + | Modifiers::CAPS_LOCK, ); if use_legacy { // Legacy text key // https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-text-keys let mut output = String::new(); - if self.modifiers.contains(Modifiers::ALT) { + if raw_modifiers.contains(Modifiers::ALT) { output.push('\x1b'); } - if self.modifiers.contains(Modifiers::CTRL) { - csi_u_encode( - &mut output, - shifted_key.to_ascii_uppercase(), - self.modifiers, - ); + if raw_modifiers.contains(Modifiers::CTRL) { + csi_u_encode(&mut output, shifted_key.to_ascii_uppercase(), raw_modifiers); } else { output.push(shifted_key); } @@ -2708,6 +2715,23 @@ mod test { .encode_kitty(flags), "\u{1b}[57399;129u".to_string() ); + assert_eq!( + make_event_with_raw( + KeyEvent { + key: KeyCode::Numpad(0), + modifiers: Modifiers::NUM_LOCK, + leds: KeyboardLedStatus::empty(), + repeat_count: 1, + key_is_down: true, + raw: None, + #[cfg(windows)] + win32_uni_char: None, + }, + Some(PhysKeyCode::Keypad0) + ) + .encode_kitty(flags), + "\u{1b}[57399;129u".to_string() + ); assert_eq!( make_event_with_raw( KeyEvent {