From e9222d1882effd62ddae9c4d81bc2d9390da7cac Mon Sep 17 00:00:00 2001 From: akbash Date: Thu, 6 Aug 2026 14:18:27 +0300 Subject: [PATCH] fix(terminal): restore keyboard reporting on detach (#2395) refs #2393 Co-authored-by: akbash-bot <300245827+akbash-bot@users.noreply.github.com> --- src/terminal_modes.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/terminal_modes.rs b/src/terminal_modes.rs index 8f0276e0..90ec512b 100644 --- a/src/terminal_modes.rs +++ b/src/terminal_modes.rs @@ -1,5 +1,7 @@ use std::io::{self, Write}; +#[cfg(not(windows))] +use crossterm::event::{PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags}; #[cfg(any(not(windows), test))] const DISABLE_HOST_MOUSE_REPORTING_SEQUENCE: &[u8] = b"\x1b[?1006l\x1b[?1016l\x1b[?1015l\x1b[?1005l\x1b[?1003l\x1b[?1002l\x1b[?1000l"; @@ -24,8 +26,13 @@ pub(crate) fn set_host_kitty_keyboard_report_all( if report_all_keys { flags |= crossterm::event::KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES; } - write!(writer, "\x1b[={}u", flags.bits())?; - writer.flush() + // Older iTerm2 releases clear the keyboard stack on SET, so a later pop + // cannot restore the host state. Replace only Herdr's top entry instead. + crossterm::execute!( + writer, + PopKeyboardEnhancementFlags, + PushKeyboardEnhancementFlags(flags) + ) } #[cfg(windows)] @@ -41,13 +48,13 @@ mod tests { use super::*; #[test] - fn host_keyboard_report_all_only_changes_the_current_herdr_stack_entry() { + fn host_keyboard_report_all_replaces_the_current_herdr_stack_entry() { let mut output = Vec::new(); set_host_kitty_keyboard_report_all(&mut output, true).unwrap(); set_host_kitty_keyboard_report_all(&mut output, false).unwrap(); - assert_eq!(output, b"\x1b[=15u\x1b[=7u"); + assert_eq!(output, b"\x1b[<1u\x1b[>15u\x1b[<1u\x1b[>7u"); } #[test]