Fix handling of shift for KeyCode::Char

This commit is contained in:
Joscha 2023-04-29 00:52:40 +02:00
parent 9bc6931fae
commit 591807dd55
2 changed files with 22 additions and 13 deletions

View file

@ -28,12 +28,6 @@ Examples of key bindings:
- `"ctrl+alt+f3"` - `"ctrl+alt+f3"`
- `["enter", "any+enter"]` (matches `enter` regardless of modifiers) - `["enter", "any+enter"]` (matches `enter` regardless of modifiers)
Available modifiers:
- `ctrl`
- `shift`
- `alt`
- `any` (matches as long as at least one modifier is pressed)
Available main keys: Available main keys:
- Any single character that can be typed - Any single character that can be typed
- `enter`, `esc` - `enter`, `esc`
@ -43,6 +37,12 @@ Available main keys:
- `home`, `end`, `pageup`, `pagedown` - `home`, `end`, `pageup`, `pagedown`
- `f1`, `f2`, ... - `f1`, `f2`, ...
Available modifiers:
- `shift` (must not be used with single characters)
- `ctrl`
- `alt`
- `any` (matches as long as at least one modifier is pressed)
## Config options ## Config options
"#; "#;

View file

@ -85,10 +85,14 @@ impl KeyPress {
} }
} }
fn parse_modifier(&mut self, modifier: &str) -> Result<(), ParseKeysError> { fn parse_modifier(
&mut self,
modifier: &str,
shift_allowed: bool,
) -> Result<(), ParseKeysError> {
match modifier { match modifier {
m if self.any => return Err(ParseKeysError::ConflictingModifier(m.to_string())), m if self.any => return Err(ParseKeysError::ConflictingModifier(m.to_string())),
"shift" if !self.shift => self.shift = true, "shift" if shift_allowed && !self.shift => self.shift = true,
"ctrl" if !self.ctrl => self.ctrl = true, "ctrl" if !self.ctrl => self.ctrl = true,
"alt" if !self.alt => self.alt = true, "alt" if !self.alt => self.alt = true,
"any" if !self.shift && !self.ctrl && !self.alt => self.any = true, "any" if !self.shift && !self.ctrl && !self.alt => self.any = true,
@ -109,10 +113,14 @@ impl KeyPress {
return true; return true;
} }
let shift = event.modifiers.contains(KeyModifiers::SHIFT); let ctrl = event.modifiers.contains(KeyModifiers::CONTROL) == self.ctrl;
let ctrl = event.modifiers.contains(KeyModifiers::CONTROL); let alt = event.modifiers.contains(KeyModifiers::ALT) == self.alt;
let alt = event.modifiers.contains(KeyModifiers::ALT); if matches!(self.code, KeyCode::Char(_)) {
self.shift == shift && self.ctrl == ctrl && self.alt == alt ctrl && alt
} else {
let shift = event.modifiers.contains(KeyModifiers::SHIFT) == self.shift;
shift && ctrl && alt
}
} }
} }
@ -124,8 +132,9 @@ impl FromStr for KeyPress {
let code = parts.next_back().ok_or(ParseKeysError::NoKeyCode)?; let code = parts.next_back().ok_or(ParseKeysError::NoKeyCode)?;
let mut keys = KeyPress::parse_key_code(code)?; let mut keys = KeyPress::parse_key_code(code)?;
let shift_allowed = !matches!(keys.code, KeyCode::Char(_));
for modifier in parts { for modifier in parts {
keys.parse_modifier(modifier)?; keys.parse_modifier(modifier, shift_allowed)?;
} }
Ok(keys) Ok(keys)