From 669416d6fa8f61d25cbde04f6237f9aa214d556d Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Sat, 26 Oct 2024 03:21:51 +0300 Subject: [PATCH] Check modifier key equality correctly --- leptos_hotkeys/src/hotkey.rs | 44 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/leptos_hotkeys/src/hotkey.rs b/leptos_hotkeys/src/hotkey.rs index a180529..1b16a07 100644 --- a/leptos_hotkeys/src/hotkey.rs +++ b/leptos_hotkeys/src/hotkey.rs @@ -98,32 +98,44 @@ pub(crate) fn is_hotkey_match( ) -> bool { let mut modifiers_match = true; + let is_ctrl_pressed = pressed_keyset.contains_key("controlleft") + || pressed_keyset.contains_key("controlright") + || pressed_keyset.contains_key("control"); if hotkey.modifiers.ctrl { - modifiers_match &= pressed_keyset.contains_key("controlleft") - || pressed_keyset.contains_key("controlright") - || pressed_keyset.contains_key("control"); + modifiers_match &= is_ctrl_pressed; + } else { + modifiers_match &= !is_ctrl_pressed; } + let is_shift_pressed = pressed_keyset.contains_key("shiftleft") + || pressed_keyset.contains_key("shiftright") + || pressed_keyset.contains_key("shift"); if hotkey.modifiers.shift { - modifiers_match &= pressed_keyset.contains_key("shiftleft") - || pressed_keyset.contains_key("shiftright") - || pressed_keyset.contains_key("shift"); + modifiers_match &= is_shift_pressed; + } else { + modifiers_match &= !is_shift_pressed; } + let is_meta_pressed = 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.meta { - 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"); + modifiers_match &= is_meta_pressed; + } else { + modifiers_match &= !is_meta_pressed; } + let is_alt_pressed = pressed_keyset.contains_key("altleft") + || pressed_keyset.contains_key("altright") + || pressed_keyset.contains_key("alt"); if hotkey.modifiers.alt { - modifiers_match &= pressed_keyset.contains_key("altleft") - || pressed_keyset.contains_key("altright") - || pressed_keyset.contains_key("alt"); + modifiers_match &= is_alt_pressed; + } else { + modifiers_match &= !is_alt_pressed; } if modifiers_match {