diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index e453a7976..e76a18ead 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -184,14 +184,19 @@ impl WindowCallbacks for TermWindow { return false; } - let mux = Mux::get().unwrap(); - if let Some(tab) = mux.get_active_tab_for_window(self.mux_window_id) { - let modifiers = window_mods_to_termwiz_mods(key.modifiers); + // log::error!("key_event {:?}", key); + enum Key { + Code(::termwiz::input::KeyCode), + Composed(String), + None, + } + + fn win_key_code_to_termwiz_key_code(key: &::window::KeyCode) -> Key { use ::termwiz::input::KeyCode as KC; use ::window::KeyCode as WK; - let key = match key.key { + let code = match key { // TODO: consider eliminating these codes from termwiz::input::KeyCode WK::Char('\r') => KC::Enter, WK::Char('\t') => KC::Tab, @@ -199,12 +204,11 @@ impl WindowCallbacks for TermWindow { WK::Char('\u{1b}') => KC::Escape, WK::Char('\u{7f}') => KC::Delete, - WK::Char(c) => KC::Char(c), + WK::Char(c) => KC::Char(*c), WK::Composed(ref s) => { - tab.writer().write_all(s.as_bytes()).ok(); - return true; + return Key::Composed(s.to_owned()); } - WK::Function(f) => KC::Function(f), + WK::Function(f) => KC::Function(*f), WK::LeftArrow => KC::LeftArrow, WK::RightArrow => KC::RightArrow, WK::UpArrow => KC::UpArrow, @@ -251,7 +255,7 @@ impl WindowCallbacks for TermWindow { WK::Numpad(7) => KC::Numpad7, WK::Numpad(8) => KC::Numpad8, WK::Numpad(9) => KC::Numpad9, - WK::Numpad(_) => return false, + WK::Numpad(_) => return Key::None, WK::Separator => KC::Separator, WK::Subtract => KC::Subtract, WK::Decimal => KC::Decimal, @@ -277,12 +281,39 @@ impl WindowCallbacks for TermWindow { WK::ApplicationUpArrow => KC::ApplicationUpArrow, WK::ApplicationDownArrow => KC::ApplicationDownArrow, }; + Key::Code(code) + } - if let Some(assignment) = self.keys.lookup(key, modifiers) { - self.perform_key_assignment(&tab, &assignment).ok(); - return true; - } else if tab.key_down(key, modifiers).is_ok() { - return true; + let mux = Mux::get().unwrap(); + if let Some(tab) = mux.get_active_tab_for_window(self.mux_window_id) { + let modifiers = window_mods_to_termwiz_mods(key.modifiers); + + // First chance to operate on the raw key; if it matches a + // user-defined key binding then we execute it and stop there. + if let Some(key) = &key.raw_key { + if let Key::Code(key) = win_key_code_to_termwiz_key_code(&key) { + if let Some(assignment) = self.keys.lookup(key, modifiers) { + self.perform_key_assignment(&tab, &assignment).ok(); + return true; + } + } + } + + let key = win_key_code_to_termwiz_key_code(&key.key); + match key { + Key::Code(key) => { + if let Some(assignment) = self.keys.lookup(key, modifiers) { + self.perform_key_assignment(&tab, &assignment).ok(); + return true; + } else if tab.key_down(key, modifiers).is_ok() { + return true; + } + } + Key::Composed(s) => { + tab.writer().write_all(s.as_bytes()).ok(); + return true; + } + Key::None => {} } } diff --git a/window/src/input.rs b/window/src/input.rs index 17f7f19e7..7865f944f 100644 --- a/window/src/input.rs +++ b/window/src/input.rs @@ -135,9 +135,15 @@ pub struct MouseEvent { #[derive(Debug, Clone, PartialEq, Eq)] pub struct KeyEvent { - /// Which key was pressed + /// Which key was pressed. + /// This is the potentially processed/composed version + /// of the input. pub key: KeyCode, + /// The raw unprocessed key press if it was different from + /// the processed/composed version + pub raw_key: Option, + /// Which modifiers are down pub modifiers: Modifiers, diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 6569960c5..5c31fd83c 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -226,6 +226,9 @@ pub(crate) struct WindowInner { } fn function_key_to_keycode(function_key: char) -> KeyCode { + // FIXME: CTRL-C is 0x3, should it be normalized to C here + // using the unmod string? Or should be normalize the 0x3 + // as the canonical representation of that input? use cocoa::appkit; match function_key as u16 { appkit::NSUpArrowFunctionKey => KeyCode::UpArrow, @@ -638,6 +641,7 @@ impl WindowView { let event = KeyEvent { key, + raw_key: None, modifiers: Modifiers::default(), repeat_count: 1, key_is_down: true, @@ -673,6 +677,7 @@ impl WindowView { let event = KeyEvent { key: KeyCode::Composed(s.to_string()), + raw_key: None, modifiers: Modifiers::default(), repeat_count: 1, key_is_down: true, @@ -866,7 +871,7 @@ impl WindowView { fn key_common(this: &mut Object, nsevent: id, key_is_down: bool) { let is_a_repeat = unsafe { nsevent.isARepeat() == YES }; let chars = unsafe { nsstring_to_str(nsevent.characters()) }; - // let unmod = unsafe { nsstring_to_str(nsevent.charactersIgnoringModifiers()) }; + let unmod = unsafe { nsstring_to_str(nsevent.charactersIgnoringModifiers()) }; let modifiers = unsafe { key_modifiers(nsevent.modifierFlags()) }; if modifiers.is_empty() && !is_a_repeat { @@ -879,21 +884,30 @@ impl WindowView { } } - let mut char_iter = chars.chars(); - if let Some(first_char) = char_iter.next() { - let key = if char_iter.next().is_none() { - // A single unicode char - function_key_to_keycode(first_char) + fn key_string_to_key_code(s: &str) -> Option { + let mut char_iter = s.chars(); + if let Some(first_char) = char_iter.next() { + if char_iter.next().is_none() { + // A single unicode char + Some(function_key_to_keycode(first_char)) + } else { + Some(KeyCode::Composed(s.to_owned())) + } } else { - KeyCode::Composed(chars.to_owned()) - }; + None + } + } - // FIXME: CTRL-C is 0x3, should it be normalized to C here - // using the unmod string? Or should be normalize the 0x3 - // as the canonical representation of that input? + if let Some(key) = key_string_to_key_code(chars) { + let raw_key = if chars == unmod { + None + } else { + key_string_to_key_code(unmod) + }; let event = KeyEvent { key, + raw_key, modifiers, repeat_count: 1, key_is_down, diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 9db27dfde..a00a7b468 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -758,6 +758,7 @@ unsafe fn ime_composition( Ok(s) => { let key = KeyEvent { key: KeyCode::Composed(s), + raw_key: None, modifiers: Modifiers::default(), repeat_count: 1, key_is_down: true, @@ -945,6 +946,7 @@ unsafe fn key(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option