GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - MonitorControl/MediaKeyTap: Access media key events from Swift.

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

72 Commits
72 Commits

Folders and files

NameName
Last commit message
Last commit date
MediaKeyTap.xcodeproj
MediaKeyTap.xcodeproj
 
 
MediaKeyTap
MediaKeyTap
 
 
MediaKeyTapExample
MediaKeyTapExample
 
 
.gitignore
.gitignore
 
 
.swift-version
.swift-version
 
 
.swiftformat
.swiftformat
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
MediaKeyTap.podspec
MediaKeyTap.podspec
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 

Repository files navigation

MediaKeyTap

MediaKeyTap provides an API for accessing the Mac's media keys (play/pause, next and previous) in your Swift application. MediaKeyTap will only capture key events when it is the most recently activated media application, matching the behaviour of existing applications, such as those using SPMediaKeyTap.

MediaKeyTap builds its whitelist by combining the static whitelist from SPMediaKeyTap with a dynamic whitelist built at runtime using NSDistributedNotificationCenter. If you create a new application using this library, you should not need to add your bundle identifier to the whitelist.

Usage

Create a MediaKeyTap:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    ⋮

    var mediaKeyTap: MediaKeyTap?

    func applicationDidFinishLaunching(aNotification: Notification) {
        mediaKeyTap = MediaKeyTap(delegate: self)
        mediaKeyTap?.start()
    }

    ⋮
}

and implement MediaKeyTapDelegate's 1 method:

func handle(mediaKey: MediaKey, event: KeyEvent) {
    switch mediaKey {
    case .playPause:
        print("Play/pause pressed")
    case .previous, .rewind:
        print("Previous pressed")
    case .next, .fastForward:
        print("Next pressed")
    }
}

You can also access the KeyEvent to access the event's underlying keycode, keyFlags and keyRepeat values.

The MediaKeyTap initialiser allows the keypress behaviour to be specified:

    MediaKeyTap(delegate: self, on: .keyDown) // Default
    MediaKeyTap(delegate: self, on: .keyUp)
    MediaKeyTap(delegate: self, on: .keyDownAndUp)

Requirements

In order to capture key events globally, you need to ensure:

  • Your application is not sandboxed
  • Your info.plist provides NSAppleEventsUsageDescription
  • Your application has accessibility privileges (will be asked automatically)

Installation

CocoaPods

Add pod 'MediaKeyTap' to your Podfile and run pod install.

Then import MediaKeyTap.

Carthage

Add github "nhurden/MediaKeyTap" to your Cartfile and run carthage update.

About

Access media key events from Swift.

Resources

Readme
MIT license
Activity
Custom properties

Stars

42 stars

Watchers

5 watching

Forks

9 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.