Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3c361f

Browse files
committedNov 4, 2024
Fix using Control+C keyboard shortcut
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.
1 parent 9203a8d commit c3c361f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed
 

‎Sources/KeyboardShortcuts/RecorderCocoa.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extension KeyboardShortcuts {
264264

265265
// The “shift” key is not allowed without other modifiers or a function key, since it doesn't actually work.
266266
guard
267-
!event.modifiers.subtracting(.shift).isEmpty
267+
!event.modifiers.subtracting([.shift, .function]).isEmpty
268268
|| event.specialKey?.isFunctionKey == true,
269269
let shortcut = Shortcut(event: event)
270270
else {

‎Sources/KeyboardShortcuts/Shortcut.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ extension KeyboardShortcuts {
6161

6262
self.init(
6363
carbonKeyCode: Int(event.keyCode),
64-
carbonModifiers: event.modifierFlags.carbon
64+
// 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.
65+
carbonModifiers: event.modifierFlags.subtracting(.function).carbon
6566
)
6667
}
6768

‎Sources/KeyboardShortcuts/Utilities.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ extension NSEvent {
152152
modifierFlags
153153
.intersection(.deviceIndependentFlagsMask)
154154
// We remove `capsLock` as it shouldn't affect the modifiers.
155-
// We remove `numericPad`/`function` as arrow keys trigger it, use `event.specialKeys` instead.
156-
.subtracting([.capsLock, .numericPad, .function])
155+
// We remove `numericPad` as arrow keys trigger it, use `event.specialKeys` instead.
156+
.subtracting([.capsLock, .numericPad])
157157
}
158158

159159
/**
@@ -262,6 +262,9 @@ enum UnicodeSymbols {
262262

263263

264264
extension NSEvent.ModifierFlags {
265+
// Not documented anywhere, but reverse-engineered by me.
266+
private static let functionKey = 1 << 17 // 131072 (0x20000)
267+
265268
var carbon: Int {
266269
var modifierFlags = 0
267270

@@ -281,6 +284,10 @@ extension NSEvent.ModifierFlags {
281284
modifierFlags |= cmdKey
282285
}
283286

287+
if contains(.function) {
288+
modifierFlags |= Self.functionKey
289+
}
290+
284291
return modifierFlags
285292
}
286293

@@ -302,6 +309,10 @@ extension NSEvent.ModifierFlags {
302309
if carbon & cmdKey == cmdKey {
303310
insert(.command)
304311
}
312+
313+
if carbon & Self.functionKey == Self.functionKey {
314+
insert(.function)
315+
}
305316
}
306317
}
307318

0 commit comments

Comments
 (0)
Please sign in to comment.