Skip to content

Commit 9fd78dc

Browse files
authored
Fix mediakeys not working everywhere, Add "none" keys option (MonitorControl#237)
1 parent 61f6fd7 commit 9fd78dc

File tree

6 files changed

+77
-57
lines changed

6 files changed

+77
-57
lines changed

MonitorControl/AppDelegate.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1818

1919
var monitorItems: [NSMenuItem] = []
2020

21-
var displayManager: DisplayManager?
2221
var mediaKeyTap: MediaKeyTap?
2322
var prefsController: NSWindowController?
2423
var keyRepeatTimers: [MediaKey: Timer] = [:]
@@ -27,15 +26,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2726

2827
func applicationDidFinishLaunching(_: Notification) {
2928
app = self
30-
31-
self.displayManager = DisplayManager()
3229
self.setupViewControllers()
3330
self.subscribeEventListeners()
31+
self.setDefaultPrefs()
3432
self.updateMediaKeyTap()
3533
self.statusItem.image = NSImage(named: "status")
3634
self.statusItem.menu = self.statusMenu
37-
self.setDefaultPrefs()
38-
Utils.acquirePrivileges()
35+
self.checkPermissions()
3936
CGDisplayRegisterReconfigurationCallback({ _, _, _ in app.updateDisplays() }, nil)
4037
self.updateDisplays()
4138
}
@@ -147,6 +144,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
147144
self.statusMenu.insertItem(monitorMenuItem, at: 0)
148145
}
149146

147+
private func checkPermissions() {
148+
let permissionsRequired: Bool = prefs.integer(forKey: Utils.PrefKeys.listenFor.rawValue) != Utils.ListenForKeys.none.rawValue
149+
if !Utils.readPrivileges(prompt: false) && permissionsRequired {
150+
Utils.acquirePrivileges()
151+
}
152+
}
153+
150154
private func setupViewControllers() {
151155
let storyboard: NSStoryboard = NSStoryboard(name: "Main", bundle: Bundle.main)
152156
let mainPrefsVc = storyboard.instantiateController(withIdentifier: "MainPrefsVC")
@@ -285,6 +289,7 @@ extension AppDelegate: MediaKeyTapDelegate {
285289
// MARK: - Prefs notification
286290

287291
@objc func handleListenForChanged() {
292+
self.checkPermissions()
288293
self.updateMediaKeyTap()
289294
}
290295

@@ -299,6 +304,7 @@ extension AppDelegate: MediaKeyTapDelegate {
299304
@objc func handlePreferenceReset() {
300305
self.setDefaultPrefs()
301306
self.updateDisplays()
307+
self.checkPermissions()
302308
self.updateMediaKeyTap()
303309
}
304310

@@ -310,6 +316,8 @@ extension AppDelegate: MediaKeyTapDelegate {
310316
keys = [.brightnessUp, .brightnessDown]
311317
case Utils.ListenForKeys.volumeOnlyKeys.rawValue:
312318
keys = [.mute, .volumeUp, .volumeDown]
319+
case Utils.ListenForKeys.none.rawValue:
320+
keys = []
313321
default:
314322
keys = [.brightnessUp, .brightnessDown, .mute, .volumeUp, .volumeDown]
315323
}
@@ -322,7 +330,7 @@ extension AppDelegate: MediaKeyTapDelegate {
322330
self.mediaKeyTap?.stop()
323331
// returning an empty array listens for all mediakeys in MediaKeyTap
324332
if keys.count > 0 {
325-
self.mediaKeyTap = MediaKeyTap(delegate: self, for: keys, observeBuiltIn: false)
333+
self.mediaKeyTap = MediaKeyTap(delegate: self, for: keys, observeBuiltIn: true)
326334
self.mediaKeyTap?.start()
327335
}
328336
}

MonitorControl/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleShortVersionString</key>
2020
<string>$(MARKETING_VERSION)</string>
2121
<key>CFBundleVersion</key>
22-
<string>642</string>
22+
<string>647</string>
2323
<key>LSApplicationCategoryType</key>
2424
<string>public.app-category.utilities</string>
2525
<key>LSMinimumSystemVersion</key>

MonitorControl/Support/Utils.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,24 @@ class Utils: NSObject {
9696

9797
/// Acquire Privileges (Necessary to listen to keyboard event globally)
9898
static func acquirePrivileges() {
99-
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: true]
100-
let accessibilityEnabled = AXIsProcessTrustedWithOptions(options)
101-
102-
if !accessibilityEnabled {
99+
if !self.readPrivileges(prompt: true) {
103100
let alert = NSAlert()
104101
alert.addButton(withTitle: NSLocalizedString("Ok", comment: "Shown in the alert dialog"))
105102
alert.messageText = NSLocalizedString("Shortcuts not available", comment: "Shown in the alert dialog")
106103
alert.informativeText = NSLocalizedString("You need to enable MonitorControl in System Preferences > Security and Privacy > Accessibility for the keyboard shortcuts to work", comment: "Shown in the alert dialog")
107104
alert.alertStyle = .warning
108105
alert.runModal()
109106
}
110-
111107
return
112108
}
113109

110+
static func readPrivileges(prompt: Bool) -> Bool {
111+
let options: NSDictionary = [kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString: prompt]
112+
let status = AXIsProcessTrustedWithOptions(options)
113+
os_log("Reading Accessibility privileges - Current access status %{public}@", type: .info, String(status))
114+
return status
115+
}
116+
114117
static func setStartAtLogin(enabled: Bool) {
115118
let identifier = "\(Bundle.main.bundleIdentifier!)Helper" as CFString
116119
SMLoginItemSetEnabled(identifier, enabled)
@@ -173,5 +176,8 @@ class Utils: NSObject {
173176

174177
/// Listen for Volume keys only
175178
case volumeOnlyKeys = 2
179+
180+
/// Don't listen for any keys
181+
case none = 3
176182
}
177183
}

0 commit comments

Comments
 (0)