7

Some mac apps, like iTunes and Spotify, react to the play/pause/next/previous buttons on some Apple keyboards.

Presumably they're tapping into some sort of NSNotification, how can I do the same?

Alex MacCaw
  • 984
  • 1
  • 6
  • 19

1 Answers1

6

I do this by subclassing NSApplication (and setting that class as the application class in my target's info) with the following:

#import <IOKit/hidsystem/ev_keymap.h>

...

- (void)mediaKeyEvent:(int)key state:(BOOL)state
{
 switch (key)
 {
  // Play pressed
  case NX_KEYTYPE_PLAY:
   if (state == NO)
    [(TSAppController *)[self delegate] togglePlayPause:self];
   break;

  // FF pressed
  case NX_KEYTYPE_FAST:
   if (state == YES)
    [(TSAppController *)[self delegate] seekForward:self];
   break;

  // RW pressed
  case NX_KEYTYPE_REWIND:
   if (state == YES)
    [(TSAppController *)[self delegate] seekBack:self];
   break;
 }
}

- (void)sendEvent:(NSEvent *)event
{
 // Catch media key events
 if ([event type] == NSSystemDefined && [event subtype] == NX_SUBTYPE_AUX_CONTROL_BUTTONS)
 {
  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];
}

The "state" in -mediaKeyEvent:state: is for up/down. In my app it makes sense to only react to the play/pause key when it's back up (done pressing), but I continuously react to RW/FF events while the key is down for seeking.

I'd love to know of a better way to do this if it exists, though. Currently, unless the user disables these keys in the global keyboard shortcuts, it controls my app and iTunes. :-)

This code has been used quite awhile in my transcription app and works well (aside from the global keyboard shortcut issue above).

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Brrr, magic numbers. You may be interested in `NX_SUBTYPE_AUX_CONTROL_BUTTONS` and `NX_KEYUPMASK`/`NSKeyUpMask` (the latter being `0x800` in the `keyFlags` variable). – Peter Hosey Mar 19 '10 at 23:22
  • Yeah, I was going to mention that very phrase but thought the "better way" encompassed it. :-) Thanks for the constants. – Joshua Nozzi Mar 20 '10 at 00:07
  • RegisterEventHotKey may let you claim control of these hotkeys? If you register a hotkey like Cmd+Space, you can stop it being handled by Dock as well as your app. – Nicholas Wilson May 31 '13 at 16:32
  • RegisterEventHotKey() is a **Carbon** call. Carbon is obsolete and these APIs should be expected to go away at any time. In fact, I'm surprised it's stayed around this long. – Joshua Nozzi May 31 '13 at 17:37