zsszatmari / MagicKeys

This is a preference pane for Mac OS X 10.7+ that enables hardware media keys (prev, play, next) to be routed to 3rd-party sandboxed applications in a nice way, without unwanted side effects. This has been specifically created with G-Ear, a native Google Music player for Mac in mind, but any suggestion or patch is welcome!
http://www.treasurebox.hu/magickeys
108 stars 13 forks source link

Document how to add MagicKeys to 3rd Party App #5

Closed plaetzchen closed 10 years ago

plaetzchen commented 10 years ago

Hi,

How can I add MagicKeys support to my music app?

Bests, Philip

zsszatmari commented 10 years ago

Dear plaetzchen,

  1. You have to add your bundle id to [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers].
  2. Integrate into your app: You can run the agent from Xcode, no need to launch actual prefpane for testing (but you should turn off the already installed prefpane with the first checkbox) In your app's playlist you have to define a 'NSPrincipalClass', which will be your own subclass of NSApplication. In this new class, override
  3. (void)sendEvent:(NSEvent *)theEvent

    Check if [theEvent type] == NSSystemDefined && [theEvent subtype] == SPSystemDefinedEventMediaKeys, then you got a media key, so: int keyCode = (([event data1] & 0xFFFF0000) >> 16); int keyFlags = ([event data1] & 0x0000FFFF); BOOL keyIsPressed = (((keyFlags & 0xFF00) >> 8)) == 0xA; int keyRepeat = (keyFlags & 0x1);

The relevant keyCodes are for play, next (which is sometimes signalled as fast), rewind (sometimes previous) keys. The following defines are needed:

define SPSystemDefinedEventMediaKeys 8

define NX_KEYTYPE_PLAY 16

define NX_KEYTYPE_NEXT 17

define NX_KEYTYPE_PREVIOUS 18

define NX_KEYTYPE_FAST 19

define NX_KEYTYPE_REWIND 20

  1. Test.
  2. If it works, I'll gladly add your bundle id to magic keys and release an update. Just tell me your bundle id or alternatively send me a pull request.

I hope this quick guide will help, but feel free to ask. Wishes, Zsolt

plaetzchen commented 10 years ago

Thanks. I've done that, please see #6

zsszatmari commented 10 years ago

Okay, I merged it, I'll do a new Magic Keys release in 1-2 days if it's fine for you!

plaetzchen commented 10 years ago

Sure, the update for the AppStore with the support will take some time anyway

zsszatmari commented 10 years ago

I thought so :) Nevertheless, I updated it, it's 1.4.1 now.