Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inconsistent Shift key behavior on Unix and Windows #174

Merged
merged 10 commits into from
Sep 20, 2023
23 changes: 18 additions & 5 deletions config/src/keymap/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::bail;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Deserialize, Eq)]
#[serde(try_from = "String")]
pub struct Key {
pub code: KeyCode,
Expand All @@ -11,6 +11,22 @@ pub struct Key {
pub alt: bool,
}

impl PartialEq for Key {
fn eq(&self, other: &Self) -> bool {
match (self.code, other.code) {
(KeyCode::Char(_), KeyCode::Char(_)) => {
self.code == other.code && self.ctrl == other.ctrl && self.alt == other.alt
}
_ => {
self.code == other.code
&& self.shift == other.shift
&& self.ctrl == other.ctrl
&& self.alt == other.alt
}
}
}
}

impl Key {
#[inline]
pub fn plain(&self) -> Option<char> {
Expand All @@ -32,11 +48,9 @@ impl Default for Key {

impl From<KeyEvent> for Key {
fn from(value: KeyEvent) -> Self {
let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase());
sxyazi marked this conversation as resolved.
Show resolved Hide resolved

Self {
code: value.code,
shift: shift || value.modifiers.contains(KeyModifiers::SHIFT),
shift: value.modifiers.contains(KeyModifiers::SHIFT),
ctrl: value.modifiers.contains(KeyModifiers::CONTROL),
alt: value.modifiers.contains(KeyModifiers::ALT),
}
Expand All @@ -55,7 +69,6 @@ impl TryFrom<String> for Key {
if !s.starts_with('<') || !s.ends_with('>') {
let c = s.chars().next().unwrap();
key.code = KeyCode::Char(c);
key.shift = c.is_ascii_uppercase();
return Ok(key);
}

Expand Down