From 578fa92a2ed9c150d7c856290a0cbc73bf09517b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 22 May 2022 06:41:57 -0700 Subject: [PATCH] win32 input mode: fixup ctrl-ascii key combinations refs: https://github.com/microsoft/terminal/issues/13134 refs: https://github.com/wez/wezterm/issues/1904 refs: https://github.com/wez/wezterm/issues/1509 refs: https://github.com/wez/wezterm/issues/2009 --- termwiz/src/input.rs | 3 ++ wezterm-input-types/src/lib.rs | 60 +++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index dac6eef8a..05c68a2f3 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -538,6 +538,9 @@ fn is_ambiguous_ascii_ctrl(c: char) -> bool { /// produced in combination with SHIFT) that may not be 100% /// the right thing to do here for users with non-US layouts. fn ctrl_mapping(c: char) -> Option { + // Please also sync with the copy of this function that + // lives in wezterm-input-types :-/ + // FIXME: move this to wezterm-input-types and take a dep on it? Some(match c { '@' | '`' | ' ' | '2' => '\x00', 'A' | 'a' => '\x01', diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 7b20a3654..15217714f 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1181,7 +1181,16 @@ impl KeyEvent { match &self.key { KeyCode::Composed(_) => None, KeyCode::Char(c) => { - let uni = *c as u32; + let c = if self.modifiers.contains(Modifiers::CTRL) { + // Ensure that we rewrite the unicode value to the ASCII CTRL + // equivalent value. + // + ctrl_mapping(*c).unwrap_or(*c) + } else { + *c + }; + let uni = c as u32; + Some(format!( "\u{1b}[{};{};{};{};{};{}_", vkey, scan_code, uni, key_down, control_key_state, self.repeat_count @@ -1234,3 +1243,52 @@ impl Default for WindowDecorations { WindowDecorations::TITLE | WindowDecorations::RESIZE } } + +/// Map c to its Ctrl equivalent. +/// In theory, this mapping is simply translating alpha characters +/// to upper case and then masking them by 0x1f, but xterm inherits +/// some built-in translation from legacy X11 so that are some +/// aliased mappings and a couple that might be technically tied +/// to US keyboard layout (particularly the punctuation characters +/// produced in combination with SHIFT) that may not be 100% +/// the right thing to do here for users with non-US layouts. +fn ctrl_mapping(c: char) -> Option { + // Please also sync with the copy of this function that + // lives in termwiz :-/ + Some(match c { + '@' | '`' | ' ' | '2' => '\x00', + 'A' | 'a' => '\x01', + 'B' | 'b' => '\x02', + 'C' | 'c' => '\x03', + 'D' | 'd' => '\x04', + 'E' | 'e' => '\x05', + 'F' | 'f' => '\x06', + 'G' | 'g' => '\x07', + 'H' | 'h' => '\x08', + 'I' | 'i' => '\x09', + 'J' | 'j' => '\x0a', + 'K' | 'k' => '\x0b', + 'L' | 'l' => '\x0c', + 'M' | 'm' => '\x0d', + 'N' | 'n' => '\x0e', + 'O' | 'o' => '\x0f', + 'P' | 'p' => '\x10', + 'Q' | 'q' => '\x11', + 'R' | 'r' => '\x12', + 'S' | 's' => '\x13', + 'T' | 't' => '\x14', + 'U' | 'u' => '\x15', + 'V' | 'v' => '\x16', + 'W' | 'w' => '\x17', + 'X' | 'x' => '\x18', + 'Y' | 'y' => '\x19', + 'Z' | 'z' => '\x1a', + '[' | '3' | '{' => '\x1b', + '\\' | '4' | '|' => '\x1c', + ']' | '5' | '}' => '\x1d', + '^' | '6' | '~' => '\x1e', + '_' | '7' | '/' => '\x1f', + '8' | '?' => '\x7f', // `Delete` + _ => return None, + }) +}