From 855957ec82a28621b8287ac595ac6decd36149c1 Mon Sep 17 00:00:00 2001 From: Jacek Swierk Date: Sat, 17 Feb 2024 13:13:01 +0100 Subject: [PATCH] fix: send correct codes with del and esc With kitty keyboard protocol enabled, both DEL and ESC key sends legacy keys - DEL sends ^H and ESC sends ^[. To be in line with kitty keyboard protocol, they should send "CSI 3 ~" and "CSI 27 u" respectively. --- wezterm-input-types/src/lib.rs | 56 +++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index b3298bad6..423126267 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1648,8 +1648,9 @@ impl KeyEvent { { // Check for simple text generating keys match &self.key { + // Don't send legacy codes with DEL and ESC + Char('\x1b') | Char('\x7f') => {} Char('\x08') => return '\x7f'.to_string(), - Char('\x7f') => return '\x08'.to_string(), Char(c) => return c.to_string(), _ => {} } @@ -3124,4 +3125,57 @@ mod test { "\u{1b}[102;14u".to_string() ); } + + #[test] + fn encode_issue_4785() { + let flags = KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES; + + assert_eq!( + KeyEvent { + key: KeyCode::Char('\x7f'), + modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), + repeat_count: 1, + key_is_down: true, + raw: None, + #[cfg(windows)] + win32_uni_char: None, + } + .encode_kitty(flags), + "\u{1b}[3;1~".to_string() + ); + + assert_eq!( + make_event_with_raw( + KeyEvent { + key: KeyCode::Char('\x7f'), + modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), + repeat_count: 1, + key_is_down: true, + raw: None, + #[cfg(windows)] + win32_uni_char: None, + }, + Some(PhysKeyCode::KeypadDelete) + ) + .encode_kitty(flags), + "\x1b[57426;1u".to_string() + ); + + assert_eq!( + KeyEvent { + key: KeyCode::Char('\x1b'), + modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), + repeat_count: 1, + key_is_down: true, + raw: None, + #[cfg(windows)] + win32_uni_char: None, + } + .encode_kitty(flags), + "\u{1b}[27;1u".to_string() + ); + } }