From 2376a561252080472743284c48d36cb83bc912c1 Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Wed, 20 Sep 2023 21:37:14 +0700 Subject: [PATCH] fix keyevent inconsistency between Windows and Linux/Mac --- config/src/keymap/key.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/config/src/keymap/key.rs b/config/src/keymap/key.rs index aabbf485..9af5047e 100644 --- a/config/src/keymap/key.rs +++ b/config/src/keymap/key.rs @@ -32,11 +32,25 @@ impl Default for Key { impl From for Key { fn from(value: KeyEvent) -> Self { - let shift = matches!(value.code, KeyCode::Char(c) if c.is_ascii_uppercase()); + /* + On Linx and Mac: + shift + alphabet => uppercase alphabet + SHIFT + shift + non alphabet => shifted non alphabet + NULL + On Windows: + shift + alphabet => uppercase alphabet + SHIFT + shift + non alphabet => shifted non alphabet + SHIFT + So we detect (non alphabet + SHIFT) and change it to (non alphabet + NULL) for consistent + behavior between OSs. + */ + let shift = match (value.code, value.modifiers) { + (KeyCode::Char(c), _) if c.is_ascii_uppercase() => true, + (KeyCode::Char(_), m) if m.contains(KeyModifiers::SHIFT) => false, + (_, m) => m.contains(KeyModifiers::SHIFT), + }; Self { code: value.code, - shift: shift || value.modifiers.contains(KeyModifiers::SHIFT), + shift, ctrl: value.modifiers.contains(KeyModifiers::CONTROL), alt: value.modifiers.contains(KeyModifiers::ALT), }