diff --git a/Sources/KeyboardShortcuts/RecorderCocoa.swift b/Sources/KeyboardShortcuts/RecorderCocoa.swift index 66e236e..0f3e017 100644 --- a/Sources/KeyboardShortcuts/RecorderCocoa.swift +++ b/Sources/KeyboardShortcuts/RecorderCocoa.swift @@ -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 { diff --git a/Sources/KeyboardShortcuts/Shortcut.swift b/Sources/KeyboardShortcuts/Shortcut.swift index 9534ce1..68c62ed 100644 --- a/Sources/KeyboardShortcuts/Shortcut.swift +++ b/Sources/KeyboardShortcuts/Shortcut.swift @@ -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 ) } diff --git a/Sources/KeyboardShortcuts/Utilities.swift b/Sources/KeyboardShortcuts/Utilities.swift index b07ae54..766618a 100644 --- a/Sources/KeyboardShortcuts/Utilities.swift +++ b/Sources/KeyboardShortcuts/Utilities.swift @@ -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]) } /** @@ -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 @@ -281,6 +284,10 @@ extension NSEvent.ModifierFlags { modifierFlags |= cmdKey } + if contains(.function) { + modifierFlags |= Self.functionKey + } + return modifierFlags } @@ -302,6 +309,10 @@ extension NSEvent.ModifierFlags { if carbon & cmdKey == cmdKey { insert(.command) } + + if carbon & Self.functionKey == Self.functionKey { + insert(.function) + } } }