From cc18f3f86ea87b1aa472d3f12627416a4408fbff Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Mon, 18 Feb 2019 20:18:44 -0500 Subject: [PATCH] Spacebar triggers play/pause --- Persephone.xcodeproj/project.pbxproj | 4 ++++ Persephone/Controllers/WindowController.swift | 14 ++++++++++++++ Persephone/Extensions/NSEvent.swift | 13 +++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 Persephone/Extensions/NSEvent.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index 879653d..6bb63c4 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ E408D3C2220E134F0006D9BE /* AlbumViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E408D3C1220E134F0006D9BE /* AlbumViewController.swift */; }; E408D3CA220E341D0006D9BE /* AlbumItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E408D3C8220E341D0006D9BE /* AlbumItem.swift */; }; E408D3CB220E341D0006D9BE /* AlbumItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = E408D3C9220E341D0006D9BE /* AlbumItem.xib */; }; + E40FE71B221B904300A4223F /* NSEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40FE71A221B904300A4223F /* NSEvent.swift */; }; E41B22C021FB6BBA00D544F6 /* libmpdclient.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (Required, ); }; }; E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; }; @@ -82,6 +83,7 @@ E408D3C1220E134F0006D9BE /* AlbumViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumViewController.swift; sourceTree = ""; }; E408D3C8220E341D0006D9BE /* AlbumItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumItem.swift; sourceTree = ""; }; E408D3C9220E341D0006D9BE /* AlbumItem.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AlbumItem.xib; sourceTree = ""; }; + E40FE71A221B904300A4223F /* NSEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSEvent.swift; sourceTree = ""; }; E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libmpdclient.2.dylib; path = libmpdclient/output/libmpdclient.2.dylib; sourceTree = ""; }; E41B22C421FB715A00D544F6 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; E41B22C521FB932700D544F6 /* MPDClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClient.swift; sourceTree = ""; }; @@ -219,6 +221,7 @@ E4928E0A2218D62A001D4BEA /* CGColor.swift */, E408D3B5220DD8970006D9BE /* Notification.swift */, E408D3B8220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift */, + E40FE71A221B904300A4223F /* NSEvent.swift */, ); path = Extensions; sourceTree = ""; @@ -489,6 +492,7 @@ buildActionMask = 2147483647; files = ( E408D3C2220E134F0006D9BE /* AlbumViewController.swift in Sources */, + E40FE71B221B904300A4223F /* NSEvent.swift in Sources */, E4928E0B2218D62A001D4BEA /* CGColor.swift in Sources */, E4A642DA22090CBE00067D21 /* Status.swift in Sources */, E4E8CC942206097F0024217A /* NotificationsController.swift in Sources */, diff --git a/Persephone/Controllers/WindowController.swift b/Persephone/Controllers/WindowController.swift index e95e853..6375a14 100644 --- a/Persephone/Controllers/WindowController.swift +++ b/Persephone/Controllers/WindowController.swift @@ -20,6 +20,11 @@ class WindowController: NSWindowController { super.windowDidLoad() window?.titleVisibility = .hidden + NSEvent.addLocalMonitorForEvents(matching: .keyDown) { + self.keyDown(with: $0) + return $0 + } + NotificationCenter.default.addObserver( self, selector: #selector(stateChanged(_:)), @@ -28,6 +33,15 @@ class WindowController: NSWindowController { ) } + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case NSEvent.keyCodeSpace: + AppDelegate.mpdClient.playPause() + default: + break + } + } + @objc func stateChanged(_ notification: Notification) { guard let state = notification.userInfo?[Notification.stateKey] as? MPDClient.Status.State else { return } diff --git a/Persephone/Extensions/NSEvent.swift b/Persephone/Extensions/NSEvent.swift new file mode 100644 index 0000000..5298e76 --- /dev/null +++ b/Persephone/Extensions/NSEvent.swift @@ -0,0 +1,13 @@ +// +// NSEvent.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/18. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Cocoa + +extension NSEvent { + static let keyCodeSpace: UInt16 = 49 +}