From 69bbbaf85d08affc8f325f3f5f9682ea40af14e3 Mon Sep 17 00:00:00 2001 From: Ryangguk Kim <13386712+rkimoakbioinformatics@users.noreply.github.com> Date: Fri, 16 Aug 2024 20:31:37 -0400 Subject: [PATCH] Use `key()` as key identifier, Fix spacebar ::trim() edgecase --------- Co-authored-by: Matthew Kim <38759997+friendlymatthew@users.noreply.github.com> --- leptos_hotkeys/src/context.rs | 4 ++-- leptos_hotkeys/src/hotkey.rs | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/leptos_hotkeys/src/context.rs b/leptos_hotkeys/src/context.rs index fbb720a..b4f221f 100644 --- a/leptos_hotkeys/src/context.rs +++ b/leptos_hotkeys/src/context.rs @@ -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); 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); diff --git a/leptos_hotkeys/src/hotkey.rs b/leptos_hotkeys/src/hotkey.rs index 6181ea1..84edb09 100644 --- a/leptos_hotkeys/src/hotkey.rs +++ b/leptos_hotkeys/src/hotkey.rs @@ -37,7 +37,7 @@ impl FromStr for Hotkey { fn from_str(key_combination: &str) -> Result { let parts = key_combination .split('+') - .map(str::trim) + .map(|v| if v == " " { v } else { v.trim() }) .collect::>(); let mut modifiers = KeyboardModifiers::default(); @@ -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 {