From 54d9278769835cfeec53a20d007903f69cb00ec1 Mon Sep 17 00:00:00 2001 From: Kami Date: Fri, 17 Jan 2025 15:41:01 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Refactor=20key=20modifier=20hand?= =?UTF-8?q?ling=20in=20Keycorder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated the logic to retrieve current modifiers by using a mapping of modifier flags to key codes. - Filtered out trigger keys from the current modifiers to ensure they do not exceed the key limit. - Fixes a bug where adding a trigger key with an arrow would cause the FN key to also be added. - Renamed variables for clarity and improved code readability. --- .../Keybind Recorder/Keycorder.swift | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Loop/Luminare/Settings/Keybindings/Keybind Recorder/Keycorder.swift b/Loop/Luminare/Settings/Keybindings/Keybind Recorder/Keycorder.swift index 6ccafb5e..e99ddca8 100644 --- a/Loop/Luminare/Settings/Keybindings/Keybind Recorder/Keycorder.swift +++ b/Loop/Luminare/Settings/Keybindings/Keybind Recorder/Keycorder.swift @@ -132,20 +132,35 @@ struct Keycorder: View { /// Get current selected keys that aren't modifiers let currentKeys = selectionKeybind + [event.keyCode] .filter { !$0.isModifier } + .map(\.baseKey) + + /// Get current modifiers that are actually pressed + let modifierMapping: [(NSEvent.ModifierFlags, CGKeyCode)] = [ + (.command, .kVK_Command), + (.option, .kVK_Option), + (.control, .kVK_Control), + (.shift, .kVK_Shift), + (.function, .kVK_Function) + ] + + let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask) + let currentModifiers = Set( + modifierMapping + .filter { flags.contains($0.0) && $0.1.isPressed } + .map(\.1) + ) + + // Filter out trigger keys + let validModifiers = currentModifiers.filter { + !Defaults[.triggerKey] + .map(\.baseModifier) + .contains($0) + } - /// Get current modifiers that aren't trigger keys - let currentModifiers = event.modifierFlags - .convertToCGKeyCode() - .filter { - !Defaults[.triggerKey] - .map(\.baseModifier) - .contains($0) - } - - let newSelection = Set(currentKeys + currentModifiers) + let finalKeys = Set(currentKeys + validModifiers) /// Make sure we don't go over the key limit - guard newSelection.count < keyLimit else { + guard finalKeys.count < keyLimit else { errorMessage = "You can only use up to \(keyLimit) keys in a keybind, including the trigger key." shouldShake.toggle() shouldError = true @@ -153,7 +168,7 @@ struct Keycorder: View { } shouldError = false - selectionKeybind = newSelection + selectionKeybind = finalKeys } func finishedObservingKeys(wasForced: Bool = false) {