Skip to content

Commit

Permalink
Use key() as key identifier, Fix spacebar ::trim() edgecase
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Matthew Kim <[email protected]>
  • Loading branch information
rkimoakbioinformatics and friendlymatthew committed Aug 17, 2024
1 parent 6adff72 commit 69bbbaf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions leptos_hotkeys/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ where
let keydown_listener =
wasm_bindgen::closure::Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
pressed_keys.update(|keys| {
keys.insert(event.code().to_lowercase(), event);
keys.insert(event.key().to_lowercase(), event);
});
}) as Box<dyn Fn(_)>);
let keyup_listener =
wasm_bindgen::closure::Closure::wrap(Box::new(move |event: web_sys::KeyboardEvent| {
pressed_keys.update(|keys| {
keys.remove(&event.code().to_lowercase());
keys.remove(&event.key().to_lowercase());
});
}) as Box<dyn Fn(_)>);

Expand Down
24 changes: 16 additions & 8 deletions leptos_hotkeys/src/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl FromStr for Hotkey {
fn from_str(key_combination: &str) -> Result<Self, Self::Err> {
let parts = key_combination
.split('+')
.map(str::trim)
.map(|v| if v == " " { v } else { v.trim() })
.collect::<Vec<&str>>();

let mut modifiers = KeyboardModifiers::default();
Expand Down Expand Up @@ -84,22 +84,30 @@ pub(crate) fn is_hotkey_match(

if hotkey.modifiers.ctrl {
modifiers_match &= pressed_keyset.contains_key("controlleft")
|| pressed_keyset.contains_key("controlright");
|| pressed_keyset.contains_key("controlright")
|| pressed_keyset.contains_key("control");
}

if hotkey.modifiers.shift {
modifiers_match &=
pressed_keyset.contains_key("shiftleft") || pressed_keyset.contains_key("shiftright");
modifiers_match &= pressed_keyset.contains_key("shiftleft")
|| pressed_keyset.contains_key("shiftright")
|| pressed_keyset.contains_key("shift");
}

if hotkey.modifiers.meta {
modifiers_match &=
pressed_keyset.contains_key("metaleft") || pressed_keyset.contains_key("metaright");
modifiers_match &= pressed_keyset.contains_key("metaleft")
|| pressed_keyset.contains_key("metaright")
|| pressed_keyset.contains_key("meta")
|| pressed_keyset.contains_key("command")
|| pressed_keyset.contains_key("cmd")
|| pressed_keyset.contains_key("super")
|| pressed_keyset.contains_key("win");
}

if hotkey.modifiers.alt {
modifiers_match &=
pressed_keyset.contains_key("altleft") || pressed_keyset.contains_key("altright");
modifiers_match &= pressed_keyset.contains_key("altleft")
|| pressed_keyset.contains_key("altright")
|| pressed_keyset.contains_key("alt");
}

if modifiers_match {
Expand Down

0 comments on commit 69bbbaf

Please sign in to comment.