From bc4ed3735d52f8054920a222189e350ded0dbf9d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 3 Sep 2021 12:07:17 -0700 Subject: [PATCH] Add SendKey key assignment refs: https://github.com/wez/wezterm/pull/1091 --- config/src/keyassignment.rs | 2 ++ config/src/keys.rs | 9 +++++++ docs/changelog.md | 1 + docs/config/lua/keyassignment/SendKey.md | 29 +++++++++++++++++++++ docs/config/lua/keyassignment/SendString.md | 1 + wezterm-gui/src/termwindow/keyevent.rs | 2 +- wezterm-gui/src/termwindow/mod.rs | 7 +++++ 7 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 docs/config/lua/keyassignment/SendKey.md diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index c15feaa27..54a99b289 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -1,3 +1,4 @@ +use crate::keys::KeyNoAction; use crate::ConfigHandle; use crate::LeaderKey; use luahelper::impl_lua_conversion; @@ -166,6 +167,7 @@ pub enum KeyAssignment { ActivateTab(isize), ActivateLastTab, SendString(String), + SendKey(KeyNoAction), Nop, DisableDefaultAssignment, Hide, diff --git a/config/src/keys.rs b/config/src/keys.rs index 9d9f1757b..5f828b740 100644 --- a/config/src/keys.rs +++ b/config/src/keys.rs @@ -4,6 +4,15 @@ use serde::{Deserialize, Deserializer, Serialize}; use std::collections::HashMap; use wezterm_input_types::{KeyCode, Modifiers}; +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] +pub struct KeyNoAction { + #[serde(deserialize_with = "de_keycode")] + pub key: KeyCode, + #[serde(deserialize_with = "de_modifiers", default)] + pub mods: Modifiers, +} +impl_lua_conversion!(KeyNoAction); + #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Key { #[serde(deserialize_with = "de_keycode")] diff --git a/docs/changelog.md b/docs/changelog.md index 8859eef54..7909cf7ef 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -26,6 +26,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: X11: jittery resize with some window managers [#1051](https://github.com/wez/wezterm/issues/1051) * Fixed: X11: [window:get_appearance](config/lua/window/get_appearance.md) now actually returns Dark when the theme is dark. [#1098](https://github.com/wez/wezterm/issues/1098) * Fixed: ALT + Arrow, PageUp/PageDown, Ins, Del, Home, End incorrectly sent ESC prefixed key sequences. [#892](https://github.com/wez/wezterm/issues/892) +* New: Added [SendKey](config/lua/keyassignment/SendKey.md) key assignment action that makes it more convenient to rebind the key input that is sent to a pane. ### 20210814-124438-54e29167 diff --git a/docs/config/lua/keyassignment/SendKey.md b/docs/config/lua/keyassignment/SendKey.md new file mode 100644 index 000000000..f3e941b60 --- /dev/null +++ b/docs/config/lua/keyassignment/SendKey.md @@ -0,0 +1,29 @@ +# SendKey + +*Since: nightly builds only* + +Send the specified key press to the current pane. This is useful to rebind +the effect of a key combination. + +Note that this rebinding effect only applies to the input that is about to be +sent to the pane; it doesn't get re-evaluated against the key assignments +you've configured in wezterm again. + +For example, macOS users often prefer to rebind `Option+LeftArrow` and +`Option+RightArrow` to match the behavior of Terminal.app, where those key +sequences are remapped to `ALT-b` and `ALT-f` which generally causes the +the cursor to move backwards or forwards by one word in most common unix +shells and applications. + +The following configuration achieves that same effect: + +```lua +local wezterm = require 'wezterm'; +return { + keys = { + -- Rebind OPT-Left, OPT-Right as ALT-b, ALT-f respectively to match Terminal.app behavior + {key="LeftArrow", mods="OPT", action={SendKey={key="b", mods="ALT"}}}, + {key="RightArrow", mods="OPT", action={SendKey={key="f", mods="ALT"}}}, + } +} +``` diff --git a/docs/config/lua/keyassignment/SendString.md b/docs/config/lua/keyassignment/SendString.md index 6107c0c0a..2d63ecb04 100644 --- a/docs/config/lua/keyassignment/SendString.md +++ b/docs/config/lua/keyassignment/SendString.md @@ -32,3 +32,4 @@ return { } ``` +See also [SendKey](SendKey.md) which makes the example above much more convenient. diff --git a/wezterm-gui/src/termwindow/keyevent.rs b/wezterm-gui/src/termwindow/keyevent.rs index 4578e0a8d..8236ebe67 100644 --- a/wezterm-gui/src/termwindow/keyevent.rs +++ b/wezterm-gui/src/termwindow/keyevent.rs @@ -238,7 +238,7 @@ impl super::TermWindow { } } - fn win_key_code_to_termwiz_key_code(&self, key: &::window::KeyCode) -> Key { + pub fn win_key_code_to_termwiz_key_code(&self, key: &::window::KeyCode) -> Key { use ::termwiz::input::KeyCode as KC; use ::window::KeyCode as WK; diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index a06fe269c..cdc2ca3e7 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -1740,6 +1740,13 @@ impl TermWindow { self.activate_tab(*n)?; } SendString(s) => pane.writer().write_all(s.as_bytes())?, + SendKey(key) => { + use keyevent::{window_mods_to_termwiz_mods, Key}; + let mods = window_mods_to_termwiz_mods(key.mods); + if let Key::Code(key) = self.win_key_code_to_termwiz_key_code(&key.key) { + pane.key_down(key, mods)?; + } + } Hide => { if let Some(w) = window.as_ref() { w.hide();