Support bindings with the Super modifier

Terminals which support the enhanced keyboard protocol send events for
keys pressed with the Super modifier (Windows/Linux key or the Command
key). The only changes that are needed to support this in Helix are:

* Mapping the modifier from crossterm's KeyModifiers to Helix's
  KeyModifiers.
* Representing and parsing the modifier from the KeyEvent text
  representation.
* Documenting the ability to remap it.
pull/6592/head
Michael Davis 1 year ago
parent 480784d2cf
commit 01bd765716
No known key found for this signature in database

@ -51,7 +51,10 @@ t = ":run-shell-command cargo test"
## Special keys and modifiers
Ctrl, Shift and Alt modifiers are encoded respectively with the prefixes
`C-`, `S-` and `A-`. Special keys are encoded as follows:
`C-`, `S-` and `A-`. The Super modifier (the Windows/Linux key or the Command
key on Mac keyboards) is also supported for terminals which support the enhanced
keyboard protocol (Kitty, WezTerm). The Super key uses the `Super-` prefix.
Special keys are encoded as follows:
| Key name | Representation |
| --- | --- |

@ -158,7 +158,12 @@ pub(crate) mod keys {
impl fmt::Display for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{}{}{}",
"{}{}{}{}",
if self.modifiers.contains(KeyModifiers::SUPER) {
"Super-"
} else {
""
},
if self.modifiers.contains(KeyModifiers::SHIFT) {
"S-"
} else {
@ -308,6 +313,9 @@ impl UnicodeWidthStr for KeyEvent {
if self.modifiers.contains(KeyModifiers::CONTROL) {
width += 2;
}
if self.modifiers.contains(KeyModifiers::SUPER) {
width += 6;
}
width
}
@ -392,6 +400,7 @@ impl std::str::FromStr for KeyEvent {
"S" => KeyModifiers::SHIFT,
"A" => KeyModifiers::ALT,
"C" => KeyModifiers::CONTROL,
"Super" => KeyModifiers::SUPER,
_ => return Err(anyhow!("Invalid key modifier '{}-'", token)),
};
@ -678,6 +687,14 @@ mod test {
modifiers: KeyModifiers::ALT | KeyModifiers::CONTROL
}
);
assert_eq!(
str::parse::<KeyEvent>("Super-c").unwrap(),
KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::SUPER
}
);
}
#[test]

@ -7,6 +7,7 @@ bitflags! {
const SHIFT = 0b0000_0001;
const CONTROL = 0b0000_0010;
const ALT = 0b0000_0100;
const SUPER = 0b0000_1000;
const NONE = 0b0000_0000;
}
}
@ -27,6 +28,9 @@ impl From<KeyModifiers> for crossterm::event::KeyModifiers {
if key_modifiers.contains(KeyModifiers::ALT) {
result.insert(CKeyModifiers::ALT);
}
if key_modifiers.contains(KeyModifiers::SUPER) {
result.insert(CKeyModifiers::SUPER);
}
result
}
@ -48,6 +52,9 @@ impl From<crossterm::event::KeyModifiers> for KeyModifiers {
if val.contains(CKeyModifiers::ALT) {
result.insert(KeyModifiers::ALT);
}
if val.contains(CKeyModifiers::SUPER) {
result.insert(KeyModifiers::SUPER);
}
result
}

Loading…
Cancel
Save