From 42f855d912cc6f8a5b5bc89225b924fd14b143e6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 21 Sep 2022 20:25:17 -0700 Subject: [PATCH] termwiz: request xterm modifyOtherKeys When in raw mode, go to level 2, but use level 1 for cooked so that we don't obfuscate ctrl-c. A consequnce of this is that CTRL-C is now reported to the app as CTRL-lowercase-c where we previously reported as CTRL-uppercase-C. Made a note of the breaking nature of this change in a new changelog file. Fixed recognizing SHIFT-TAB refs: https://github.com/wez/wezterm/issues/2511 --- termwiz/CHANGELOG.md | 7 +++++++ termwiz/examples/key_tester.rs | 2 +- termwiz/src/input.rs | 11 +++++++++-- termwiz/src/terminal/unix.rs | 16 +++++++++++++++- 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 termwiz/CHANGELOG.md diff --git a/termwiz/CHANGELOG.md b/termwiz/CHANGELOG.md new file mode 100644 index 000000000..b099a795b --- /dev/null +++ b/termwiz/CHANGELOG.md @@ -0,0 +1,7 @@ + +main: + +* BREAKING: We now request modifyOtherKeys when setting up the unix terminal. + As a consequence, CTRL keys like `CTRL-C` are now reported as + `CTRL-lower-case-c` rather than `CTRL-upper-case-C`. We do this even when + modifyOtherKeys isn't active for the sake of overall consistency. diff --git a/termwiz/examples/key_tester.rs b/termwiz/examples/key_tester.rs index 861fad78e..11e76a2d0 100644 --- a/termwiz/examples/key_tester.rs +++ b/termwiz/examples/key_tester.rs @@ -4,7 +4,7 @@ use termwiz::terminal::{new_terminal, Terminal}; use termwiz::Error; const CTRL_C: KeyEvent = KeyEvent { - key: KeyCode::Char('C'), + key: KeyCode::Char('c'), modifiers: Modifiers::CTRL, }; diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index da252acab..9a00edf02 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -1167,7 +1167,7 @@ impl InputParser { map.insert( &ctrl, InputEvent::Key(KeyEvent { - key: KeyCode::Char(alpha as char), + key: KeyCode::Char((alpha as char).to_ascii_lowercase()), modifiers: Modifiers::CTRL, }), ); @@ -1416,6 +1416,13 @@ impl InputParser { modifiers: Modifiers::NONE, }), ); + map.insert( + b"\x1b[Z", + InputEvent::Key(KeyEvent { + key: KeyCode::Tab, + modifiers: Modifiers::SHIFT, + }), + ); map.insert( &[b'\r'], @@ -1724,7 +1731,7 @@ mod test { vec![ InputEvent::Key(KeyEvent { modifiers: Modifiers::CTRL, - key: KeyCode::Char('C'), + key: KeyCode::Char('c'), }), InputEvent::Key(KeyEvent { modifiers: Modifiers::ALT, diff --git a/termwiz/src/terminal/unix.rs b/termwiz/src/terminal/unix.rs index f59e29ac5..fc405487c 100644 --- a/termwiz/src/terminal/unix.rs +++ b/termwiz/src/terminal/unix.rs @@ -18,7 +18,7 @@ use termios::{ }; use crate::caps::Capabilities; -use crate::escape::csi::{DecPrivateMode, DecPrivateModeCode, Mode, CSI}; +use crate::escape::csi::{DecPrivateMode, DecPrivateModeCode, Mode, XtermKeyModifierResource, CSI}; use crate::input::{InputEvent, InputParser}; use crate::render::terminfo::TerminfoRenderer; use crate::surface::Change; @@ -99,6 +99,17 @@ impl TtyWriteHandle { } Ok(()) } + + fn modify_other_keys(&mut self, level: i64) -> std::io::Result<()> { + write!( + self, + "{}", + CSI::Mode(Mode::XtermKeyMode { + resource: XtermKeyModifierResource::OtherKeys, + value: Some(level), + }) + ) + } } impl Write for TtyWriteHandle { @@ -323,12 +334,14 @@ impl Terminal for UnixTerminal { decset!(AnyEventMouse); decset!(SGRMouse); } + self.write.modify_other_keys(2)?; self.write.flush()?; Ok(()) } fn set_cooked_mode(&mut self) -> Result<()> { + self.write.modify_other_keys(1)?; self.write .set_termios(&self.saved_termios, SetAttributeWhen::Now) } @@ -515,6 +528,7 @@ impl Drop for UnixTerminal { decreset!(SGRMouse); decreset!(AnyEventMouse); } + self.write.modify_other_keys(0).unwrap(); self.exit_alternate_screen().unwrap(); self.write.flush().unwrap();