Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3210782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:37:09+00:00 2026-05-17T14:37:09+00:00

Is there a way to make my app respond to the play/pause button on

  • 0

Is there a way to make my app respond to the play/pause button on Mac?

EDIT:

Using the suggested code,I get this console message:

Could not connect the action buttonPressed: to target of class NSApplication

Why would that be?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:37:10+00:00Added an answer on May 17, 2026 at 2:37 pm

    I accomplished this in my own application by subclassing NSApplication (and setting the app’s principal class to this subclass). It catches seek and play/pause keys and translates them to specific actions in my app delegate.

    Relevant lines:

    #import <IOKit/hidsystem/ev_keymap.h>
    
    - (void)sendEvent:(NSEvent *)event
    {
        // Catch media key events
        if ([event type] == NSSystemDefined && [event subtype] == 8)
        {
            int keyCode = (([event data1] & 0xFFFF0000) >> 16);
            int keyFlags = ([event data1] & 0x0000FFFF);
            int keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA;
    
            // Process the media key event and return
            [self mediaKeyEvent:keyCode state:keyState];
            return;
        }
    
        // Continue on to super
        [super sendEvent:event];
    }
    
    - (void)mediaKeyEvent:(int)key state:(BOOL)state
    {
        switch (key)
        {
            // Play pressed
            case NX_KEYTYPE_PLAY:
                if (state == NO)
                    [(TSAppController *)[self delegate] togglePlayPause:self];
                break;
    
            // Rewind
            case NX_KEYTYPE_FAST:
                if (state == YES)
                    [(TSAppController *)[self delegate] seekForward:self];
                break;
    
            // Previous
            case NX_KEYTYPE_REWIND:
                if (state == YES)
                    [(TSAppController *)[self delegate] seekBack:self];
                break;
        }
    }
    

    Edit:

    Swift 4:

    override func sendEvent(_ event: NSEvent)
    {
        if  event.type == .systemDefined &&
            event.subtype == .screenChanged
        {
            let keyCode : Int32 = (Int32((event.data1 & 0xFFFF0000) >> 16))
            let keyFlags = (event.data1 & 0x0000FFFF)
            let keyState = ((keyFlags & 0xFF00) >> 8) == 0xA
    
            self.mediaKeyEvent(withKeyCode: keyCode, andState: keyState)
            return
        }
    
        super.sendEvent(event)
    }
    
    private func mediaKeyEvent(withKeyCode keyCode : Int32, andState state : Bool)
    {
        guard let delegate = self.delegate as? AppDelegate else { return }
    
        switch keyCode
        {
            // Play pressed
            case NX_KEYTYPE_PLAY:
                if state == false
                {
                    delegate.musicPlayerWC.handleUserPressedPlayButton()
                }
                break
            // Rewind
            case NX_KEYTYPE_FAST:
                if state == true
                {
                    delegate.musicPlayerWC.handleUserPressedNextSongButton()
                }
                break
    
            // Previous
            case NX_KEYTYPE_REWIND:
                if state == true
                {
                    delegate.musicPlayerWC.handleUserPressedPreviousSongButton()
                }
    
                break
            default:
                break
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make this code work? LogonControl.java @Audit(AuditType.LOGON) public void login(String
Is there a way to make Windows Mobile not use the main phone app?
Is there a way to make a click-to-edit control in silverlight? I've got some
Is there a way to make this line shorter? bool pass = d !=
Is there any way to make a call from within an app without quitting
Is there a truefire way to make sure my app will display the same
Is there any way to make my app send to me some additional app
Is there any way to make a telnet app to clear the output client-side
quick question...is there a way to make update to an iphone app (news and
Is there a way to make an Adobe AIR app fit screen(maximize) by itself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.