Skip to content

Commit

Permalink
Fix using Control+C keyboard shortcut
Browse files Browse the repository at this point in the history
It was incorrectly triggering the dialog for it being taken by the system. This is because we were not storing the `.function` modifier in the shortcut. The system shortcut is Fn+Control+C.
  • Loading branch information
sindresorhus committed Nov 4, 2024
1 parent 9203a8d commit c3c361f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/KeyboardShortcuts/RecorderCocoa.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ extension KeyboardShortcuts {

// The “shift” key is not allowed without other modifiers or a function key, since it doesn't actually work.
guard
!event.modifiers.subtracting(.shift).isEmpty
!event.modifiers.subtracting([.shift, .function]).isEmpty
|| event.specialKey?.isFunctionKey == true,
let shortcut = Shortcut(event: event)
else {
Expand Down
3 changes: 2 additions & 1 deletion Sources/KeyboardShortcuts/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ extension KeyboardShortcuts {

self.init(
carbonKeyCode: Int(event.keyCode),
carbonModifiers: event.modifierFlags.carbon
// Note: We could potentially support users specifying shortcuts with the Fn key, but I haven't found a reliable way to differentate when to display the Fn key and not. For example, with Fn+F1 we only want to display F1, but with Fn+V, we want to display both. I cannot just specialize it for F keys as it applies to other keys too, like Fn+arrowup.
carbonModifiers: event.modifierFlags.subtracting(.function).carbon
)
}

Expand Down
15 changes: 13 additions & 2 deletions Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,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 @@ -262,6 +262,9 @@ enum UnicodeSymbols {


extension NSEvent.ModifierFlags {
// Not documented anywhere, but reverse-engineered by me.
private static let functionKey = 1 << 17 // 131072 (0x20000)

var carbon: Int {
var modifierFlags = 0

Expand All @@ -281,6 +284,10 @@ extension NSEvent.ModifierFlags {
modifierFlags |= cmdKey
}

if contains(.function) {
modifierFlags |= Self.functionKey
}

return modifierFlags
}

Expand All @@ -302,6 +309,10 @@ extension NSEvent.ModifierFlags {
if carbon & cmdKey == cmdKey {
insert(.command)
}

if carbon & Self.functionKey == Self.functionKey {
insert(.function)
}
}
}

Expand Down

0 comments on commit c3c361f

Please sign in to comment.