Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 Refactor key modifier handling in Keycorder #675

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions Loop/Luminare/Settings/Keybindings/Keybind Recorder/Keycorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,43 @@ 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
return
}

shouldError = false
selectionKeybind = newSelection
selectionKeybind = finalKeys
}

func finishedObservingKeys(wasForced: Bool = false) {
Expand Down