Skip to main content

halley_config/keybinds/
chord.rs

1use super::{KeyModifiers, key_name_to_evdev};
2
3pub fn parse_chord(chord: &str) -> Option<(KeyModifiers, u32)> {
4    let mut mods = KeyModifiers::default();
5    let mut key: Option<u32> = None;
6
7    for raw in chord.split('+') {
8        let t = raw.trim();
9        if t.is_empty() {
10            continue;
11        }
12
13        if apply_modifier_token(&mut mods, t) {
14            continue;
15        }
16
17        if key.is_some() {
18            return None;
19        }
20
21        key = key_name_to_evdev(t);
22    }
23
24    key.map(|k| (mods, k))
25}
26
27fn apply_modifier_token(mods: &mut KeyModifiers, token: &str) -> bool {
28    match token.trim().to_ascii_lowercase().as_str() {
29        "lalt" => {
30            mods.left_alt = true;
31            true
32        }
33        "ralt" => {
34            mods.right_alt = true;
35            true
36        }
37        "alt" => {
38            mods.alt = true;
39            true
40        }
41        "lshift" => {
42            mods.left_shift = true;
43            true
44        }
45        "rshift" => {
46            mods.right_shift = true;
47            true
48        }
49        "shift" => {
50            mods.shift = true;
51            true
52        }
53        "lctrl" => {
54            mods.left_ctrl = true;
55            true
56        }
57        "rctrl" => {
58            mods.right_ctrl = true;
59            true
60        }
61        "ctrl" | "control" => {
62            mods.ctrl = true;
63            true
64        }
65        "lsuper" | "lwin" => {
66            mods.left_super = true;
67            true
68        }
69        "rsuper" | "rwin" => {
70            mods.right_super = true;
71            true
72        }
73        "super" | "win" | "windows" | "logo" | "meta" => {
74            mods.super_key = true;
75            true
76        }
77        _ => false,
78    }
79}