Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 8, 2024
1 parent 9369a04 commit 16e7f8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 11 additions & 7 deletions Sources/KeyboardShortcuts/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,20 @@ extension KeyboardShortcuts.Shortcut {
Check whether the keyboard shortcut is disallowed.
*/
var isDisallowed: Bool {
let disallowedModifiers: NSEvent.ModifierFlags = [.option, [.option, .shift]]
if
guard
#available(macOS 15, *),
Constants.isSandboxed,
disallowedModifiers.contains(modifiers)
{
return true
Constants.isSandboxed
else {
return false
}

if !modifiers.contains(.option) {
return false // Allowed if Option is not involved
}

return false
// If Option is present, ensure there's at least one modifier other than Option and Shift
let otherModifiers: NSEvent.ModifierFlags = [.command, .control, .function, .capsLock]
return !modifiers.isDisjoint(with: otherModifiers)
}

/**
Expand Down
13 changes: 11 additions & 2 deletions Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ extension NSEvent {
modifierFlags
.intersection(.deviceIndependentFlagsMask)
// We remove `capsLock` as it shouldn't affect the modifiers.
// We remove `numericPad`/`function` as arrow keys trigger it, use `event.specialKeys` instead.
.subtracting([.capsLock, .numericPad, .function])
// We remove `numericPad` as arrow keys trigger it, use `event.specialKeys` instead.
.subtracting([.capsLock, .numericPad])
}
}

Expand Down Expand Up @@ -305,6 +305,11 @@ extension NSEvent.ModifierFlags {
}
}

extension SwiftUI.EventModifiers {
// `.function` is deprecated, so we use the raw value.
fileprivate static let function_nonDeprecated = Self(rawValue: 64)
}

extension NSEvent.ModifierFlags {
var toEventModifiers: SwiftUI.EventModifiers {
var modifiers = SwiftUI.EventModifiers()
Expand Down Expand Up @@ -333,6 +338,10 @@ extension NSEvent.ModifierFlags {
modifiers.insert(.shift)
}

if contains(.function) {
modifiers.insert(.function_nonDeprecated)
}

return modifiers
}
}
Expand Down

0 comments on commit 16e7f8b

Please sign in to comment.