Skip to content

Commit

Permalink
Handle disambiguated keycodes
Browse files Browse the repository at this point in the history
Media keys are sent despite `DISAMBIGUATE_ESCAPE_CODES` being unset.
Previously we panicked on these. This change translates the
disambiguated keys from crossterm so that they do not cause a panic.
  • Loading branch information
the-mikedavis committed Nov 25, 2022
1 parent 2271c3a commit e9fa952
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 12 deletions.
142 changes: 141 additions & 1 deletion helix-view/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidth
use serde::de::{self, Deserialize, Deserializer};
use std::fmt;

pub use crate::keyboard::{KeyCode, KeyModifiers};
pub use crate::keyboard::{KeyCode, KeyModifiers, MediaKeyCode, ModifierKeyCode};

#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)]
pub enum Event {
Expand Down Expand Up @@ -119,6 +119,40 @@ pub(crate) mod keys {
pub(crate) const MINUS: &str = "minus";
pub(crate) const LESS_THAN: &str = "lt";
pub(crate) const GREATER_THAN: &str = "gt";
pub(crate) const CAPS_LOCK: &str = "capslock";
pub(crate) const SCROLL_LOCK: &str = "scrolllock";
pub(crate) const NUM_LOCK: &str = "numlock";
pub(crate) const PRINT_SCREEN: &str = "printscreen";
pub(crate) const PAUSE: &str = "pause";
pub(crate) const MENU: &str = "menu";
pub(crate) const KEYPAD_BEGIN: &str = "keypadbegin";
pub(crate) const PLAY: &str = "play";
pub(crate) const PAUSE_MEDIA: &str = "pausemedia";
pub(crate) const PLAY_PAUSE: &str = "playpause";
pub(crate) const REVERSE: &str = "reverse";
pub(crate) const STOP: &str = "stop";
pub(crate) const FAST_FORWARD: &str = "fastforward";
pub(crate) const REWIND: &str = "rewind";
pub(crate) const TRACK_NEXT: &str = "tracknext";
pub(crate) const TRACK_PREVIOUS: &str = "trackprevious";
pub(crate) const RECORD: &str = "record";
pub(crate) const LOWER_VOLUME: &str = "lowervolume";
pub(crate) const RAISE_VOLUME: &str = "raisevolume";
pub(crate) const MUTE_VOLUME: &str = "mutevolume";
pub(crate) const LEFT_SHIFT: &str = "leftshift";
pub(crate) const LEFT_CONTROL: &str = "leftcontrol";
pub(crate) const LEFT_ALT: &str = "leftalt";
pub(crate) const LEFT_SUPER: &str = "leftsuper";
pub(crate) const LEFT_HYPER: &str = "lefthyper";
pub(crate) const LEFT_META: &str = "leftmeta";
pub(crate) const RIGHT_SHIFT: &str = "rightshift";
pub(crate) const RIGHT_CONTROL: &str = "rightcontrol";
pub(crate) const RIGHT_ALT: &str = "rightalt";
pub(crate) const RIGHT_SUPER: &str = "rightsuper";
pub(crate) const RIGHT_HYPER: &str = "righthyper";
pub(crate) const RIGHT_META: &str = "rightmeta";
pub(crate) const ISO_LEVEL_3_SHIFT: &str = "isolevel3shift";
pub(crate) const ISO_LEVEL_5_SHIFT: &str = "isolevel5shift";
}

impl fmt::Display for KeyEvent {
Expand Down Expand Up @@ -163,6 +197,44 @@ impl fmt::Display for KeyEvent {
KeyCode::Char('>') => f.write_str(keys::GREATER_THAN)?,
KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?,
KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?,
KeyCode::CapsLock => f.write_str(keys::CAPS_LOCK)?,
KeyCode::ScrollLock => f.write_str(keys::SCROLL_LOCK)?,
KeyCode::NumLock => f.write_str(keys::NUM_LOCK)?,
KeyCode::PrintScreen => f.write_str(keys::PRINT_SCREEN)?,
KeyCode::Pause => f.write_str(keys::PAUSE)?,
KeyCode::Menu => f.write_str(keys::MENU)?,
KeyCode::KeypadBegin => f.write_str(keys::KEYPAD_BEGIN)?,
KeyCode::Media(MediaKeyCode::Play) => f.write_str(keys::PLAY)?,
KeyCode::Media(MediaKeyCode::Pause) => f.write_str(keys::PAUSE_MEDIA)?,
KeyCode::Media(MediaKeyCode::PlayPause) => f.write_str(keys::PLAY_PAUSE)?,
KeyCode::Media(MediaKeyCode::Stop) => f.write_str(keys::STOP)?,
KeyCode::Media(MediaKeyCode::Reverse) => f.write_str(keys::REVERSE)?,
KeyCode::Media(MediaKeyCode::FastForward) => f.write_str(keys::FAST_FORWARD)?,
KeyCode::Media(MediaKeyCode::Rewind) => f.write_str(keys::REWIND)?,
KeyCode::Media(MediaKeyCode::TrackNext) => f.write_str(keys::TRACK_NEXT)?,
KeyCode::Media(MediaKeyCode::TrackPrevious) => f.write_str(keys::TRACK_PREVIOUS)?,
KeyCode::Media(MediaKeyCode::Record) => f.write_str(keys::RECORD)?,
KeyCode::Media(MediaKeyCode::LowerVolume) => f.write_str(keys::LOWER_VOLUME)?,
KeyCode::Media(MediaKeyCode::RaiseVolume) => f.write_str(keys::RAISE_VOLUME)?,
KeyCode::Media(MediaKeyCode::MuteVolume) => f.write_str(keys::MUTE_VOLUME)?,
KeyCode::Modifier(ModifierKeyCode::LeftShift) => f.write_str(keys::LEFT_SHIFT)?,
KeyCode::Modifier(ModifierKeyCode::LeftControl) => f.write_str(keys::LEFT_CONTROL)?,
KeyCode::Modifier(ModifierKeyCode::LeftAlt) => f.write_str(keys::LEFT_ALT)?,
KeyCode::Modifier(ModifierKeyCode::LeftSuper) => f.write_str(keys::LEFT_SUPER)?,
KeyCode::Modifier(ModifierKeyCode::LeftHyper) => f.write_str(keys::LEFT_HYPER)?,
KeyCode::Modifier(ModifierKeyCode::LeftMeta) => f.write_str(keys::LEFT_META)?,
KeyCode::Modifier(ModifierKeyCode::RightShift) => f.write_str(keys::RIGHT_SHIFT)?,
KeyCode::Modifier(ModifierKeyCode::RightControl) => f.write_str(keys::RIGHT_CONTROL)?,
KeyCode::Modifier(ModifierKeyCode::RightAlt) => f.write_str(keys::RIGHT_ALT)?,
KeyCode::Modifier(ModifierKeyCode::RightSuper) => f.write_str(keys::RIGHT_SUPER)?,
KeyCode::Modifier(ModifierKeyCode::RightHyper) => f.write_str(keys::RIGHT_HYPER)?,
KeyCode::Modifier(ModifierKeyCode::RightMeta) => f.write_str(keys::RIGHT_META)?,
KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift) => {
f.write_str(keys::ISO_LEVEL_3_SHIFT)?
}
KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift) => {
f.write_str(keys::ISO_LEVEL_5_SHIFT)?
}
};
Ok(())
}
Expand Down Expand Up @@ -192,6 +264,40 @@ impl UnicodeWidthStr for KeyEvent {
KeyCode::F(1..=9) => 2,
KeyCode::F(_) => 3,
KeyCode::Char(c) => c.width().unwrap_or(0),
KeyCode::CapsLock => keys::CAPS_LOCK.len(),
KeyCode::ScrollLock => keys::SCROLL_LOCK.len(),
KeyCode::NumLock => keys::NUM_LOCK.len(),
KeyCode::PrintScreen => keys::PRINT_SCREEN.len(),
KeyCode::Pause => keys::PAUSE.len(),
KeyCode::Menu => keys::MENU.len(),
KeyCode::KeypadBegin => keys::KEYPAD_BEGIN.len(),
KeyCode::Media(MediaKeyCode::Play) => keys::PLAY.len(),
KeyCode::Media(MediaKeyCode::Pause) => keys::PAUSE_MEDIA.len(),
KeyCode::Media(MediaKeyCode::PlayPause) => keys::PLAY_PAUSE.len(),
KeyCode::Media(MediaKeyCode::Stop) => keys::STOP.len(),
KeyCode::Media(MediaKeyCode::Reverse) => keys::REVERSE.len(),
KeyCode::Media(MediaKeyCode::FastForward) => keys::FAST_FORWARD.len(),
KeyCode::Media(MediaKeyCode::Rewind) => keys::REWIND.len(),
KeyCode::Media(MediaKeyCode::TrackNext) => keys::TRACK_NEXT.len(),
KeyCode::Media(MediaKeyCode::TrackPrevious) => keys::TRACK_PREVIOUS.len(),
KeyCode::Media(MediaKeyCode::Record) => keys::RECORD.len(),
KeyCode::Media(MediaKeyCode::LowerVolume) => keys::LOWER_VOLUME.len(),
KeyCode::Media(MediaKeyCode::RaiseVolume) => keys::RAISE_VOLUME.len(),
KeyCode::Media(MediaKeyCode::MuteVolume) => keys::MUTE_VOLUME.len(),
KeyCode::Modifier(ModifierKeyCode::LeftShift) => keys::LEFT_SHIFT.len(),
KeyCode::Modifier(ModifierKeyCode::LeftControl) => keys::LEFT_CONTROL.len(),
KeyCode::Modifier(ModifierKeyCode::LeftAlt) => keys::LEFT_ALT.len(),
KeyCode::Modifier(ModifierKeyCode::LeftSuper) => keys::LEFT_SUPER.len(),
KeyCode::Modifier(ModifierKeyCode::LeftHyper) => keys::LEFT_HYPER.len(),
KeyCode::Modifier(ModifierKeyCode::LeftMeta) => keys::LEFT_META.len(),
KeyCode::Modifier(ModifierKeyCode::RightShift) => keys::RIGHT_SHIFT.len(),
KeyCode::Modifier(ModifierKeyCode::RightControl) => keys::RIGHT_CONTROL.len(),
KeyCode::Modifier(ModifierKeyCode::RightAlt) => keys::RIGHT_ALT.len(),
KeyCode::Modifier(ModifierKeyCode::RightSuper) => keys::RIGHT_SUPER.len(),
KeyCode::Modifier(ModifierKeyCode::RightHyper) => keys::RIGHT_HYPER.len(),
KeyCode::Modifier(ModifierKeyCode::RightMeta) => keys::RIGHT_META.len(),
KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift) => keys::ISO_LEVEL_3_SHIFT.len(),
KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift) => keys::ISO_LEVEL_5_SHIFT.len(),
};
if self.modifiers.contains(KeyModifiers::SHIFT) {
width += 2;
Expand Down Expand Up @@ -235,6 +341,40 @@ impl std::str::FromStr for KeyEvent {
keys::MINUS => KeyCode::Char('-'),
keys::LESS_THAN => KeyCode::Char('<'),
keys::GREATER_THAN => KeyCode::Char('>'),
keys::CAPS_LOCK => KeyCode::CapsLock,
keys::SCROLL_LOCK => KeyCode::ScrollLock,
keys::NUM_LOCK => KeyCode::NumLock,
keys::PRINT_SCREEN => KeyCode::PrintScreen,
keys::PAUSE => KeyCode::Pause,
keys::MENU => KeyCode::Menu,
keys::KEYPAD_BEGIN => KeyCode::KeypadBegin,
keys::PLAY => KeyCode::Media(MediaKeyCode::Play),
keys::PAUSE_MEDIA => KeyCode::Media(MediaKeyCode::Pause),
keys::PLAY_PAUSE => KeyCode::Media(MediaKeyCode::PlayPause),
keys::STOP => KeyCode::Media(MediaKeyCode::Stop),
keys::REVERSE => KeyCode::Media(MediaKeyCode::Reverse),
keys::FAST_FORWARD => KeyCode::Media(MediaKeyCode::FastForward),
keys::REWIND => KeyCode::Media(MediaKeyCode::Rewind),
keys::TRACK_NEXT => KeyCode::Media(MediaKeyCode::TrackNext),
keys::TRACK_PREVIOUS => KeyCode::Media(MediaKeyCode::TrackPrevious),
keys::RECORD => KeyCode::Media(MediaKeyCode::Record),
keys::LOWER_VOLUME => KeyCode::Media(MediaKeyCode::LowerVolume),
keys::RAISE_VOLUME => KeyCode::Media(MediaKeyCode::RaiseVolume),
keys::MUTE_VOLUME => KeyCode::Media(MediaKeyCode::MuteVolume),
keys::LEFT_SHIFT => KeyCode::Modifier(ModifierKeyCode::LeftShift),
keys::LEFT_CONTROL => KeyCode::Modifier(ModifierKeyCode::LeftControl),
keys::LEFT_ALT => KeyCode::Modifier(ModifierKeyCode::LeftAlt),
keys::LEFT_SUPER => KeyCode::Modifier(ModifierKeyCode::LeftSuper),
keys::LEFT_HYPER => KeyCode::Modifier(ModifierKeyCode::LeftHyper),
keys::LEFT_META => KeyCode::Modifier(ModifierKeyCode::LeftMeta),
keys::RIGHT_SHIFT => KeyCode::Modifier(ModifierKeyCode::RightShift),
keys::RIGHT_CONTROL => KeyCode::Modifier(ModifierKeyCode::RightControl),
keys::RIGHT_ALT => KeyCode::Modifier(ModifierKeyCode::RightAlt),
keys::RIGHT_SUPER => KeyCode::Modifier(ModifierKeyCode::RightSuper),
keys::RIGHT_HYPER => KeyCode::Modifier(ModifierKeyCode::RightHyper),
keys::RIGHT_META => KeyCode::Modifier(ModifierKeyCode::RightMeta),
keys::ISO_LEVEL_3_SHIFT => KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift),
keys::ISO_LEVEL_5_SHIFT => KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift),
single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()),
function if function.len() > 1 && function.starts_with('F') => {
let function: String = function.chars().skip(1).collect();
Expand Down
Loading

0 comments on commit e9fa952

Please sign in to comment.