Skip to content

Commit

Permalink
Allow using F12 as a shortcut
Browse files Browse the repository at this point in the history
The system API says it’s reserved, but it doesn’t seem to be used for anything.
  • Loading branch information
sindresorhus committed Jun 7, 2021
1 parent 199c3b2 commit a7a3ff6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/KeyboardShortcuts/KeyboardShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum KeyboardShortcuts {

/**
Remove all handlers receiving keyboard shortcuts events.

This can be used to reset the handlers before re-creating them to avoid having multiple handlers for the same shortcut.
*/
public static func removeAllHandlers() {
Expand Down
4 changes: 2 additions & 2 deletions Sources/KeyboardShortcuts/NSMenuItem++.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa

extension NSMenuItem {
private struct AssociatedKeys {
private enum AssociatedKeys {
static let observer = ObjectAssociation<NSObjectProtocol>()
}

Expand Down Expand Up @@ -68,7 +68,7 @@ extension NSMenuItem {

/**
Add a keyboard shortcut to a `NSMenuItem`.

This method is only recommended for dynamic shortcuts. In general, it's preferred to create a static shortcut name and use `NSMenuItem.setShortcut(for:)` instead.

Pass in `nil` to clear the keyboard shortcut.
Expand Down
12 changes: 6 additions & 6 deletions Sources/KeyboardShortcuts/RecorderCocoa.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ extension KeyboardShortcuts {
self.placeholderString = "record_shortcut".localized
self.centersPlaceholder = true
self.alignment = .center
(self.cell as? NSSearchFieldCell)?.searchButtonCell = nil
(cell as? NSSearchFieldCell)?.searchButtonCell = nil

self.wantsLayer = true
self.translatesAutoresizingMaskIntoConstraints = false
self.setContentHuggingPriority(.defaultHigh, for: .vertical)
self.setContentHuggingPriority(.defaultHigh, for: .horizontal)
self.widthAnchor.constraint(greaterThanOrEqualToConstant: CGFloat(minimumWidth)).isActive = true
setContentHuggingPriority(.defaultHigh, for: .vertical)
setContentHuggingPriority(.defaultHigh, for: .horizontal)
widthAnchor.constraint(greaterThanOrEqualToConstant: CGFloat(minimumWidth)).isActive = true

// Hide the cancel button when not showing the shortcut so the placeholder text is properly centered. Must be last.
self.cancelButton = (self.cell as? NSSearchFieldCell)?.cancelButtonCell
self.cancelButton = (cell as? NSSearchFieldCell)?.cancelButtonCell

self.setStringValue(name: name)
setStringValue(name: name)

setUpEvents()
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/KeyboardShortcuts/Shortcut.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ extension KeyboardShortcuts {

extension KeyboardShortcuts.Shortcut {
/// System-defined keyboard shortcuts.
static var system: [Self] { CarbonKeyboardShortcuts.system }
static var system: [Self] {
CarbonKeyboardShortcuts.system
}

/// Check whether the keyboard shortcut is already taken by the system.
var isTakenBySystem: Bool { Self.system.contains(self) }
var isTakenBySystem: Bool {
guard self != KeyboardShortcuts.Shortcut(.f12, modifiers: []) else {
return false
}

return Self.system.contains(self)
}
}

extension KeyboardShortcuts.Shortcut {
Expand Down

0 comments on commit a7a3ff6

Please sign in to comment.