There are many articles about the topic How do I change the behavior of my Apple keyboard in relation to the Media Keys and iTunes. There are also many Cocoa code examples on how the Media Keys can be used in a Mac application. But all solutions I found on the Internet were too flat for me.
Many articles have been described that the so-called Remote Control Daemon (RCD) is responsible for launching iTunes automatically every time you press the Play Button on your Apple Keyboard. That’s correct — but I think you should not kill the RCD process with
killall and much less patching it with a shady tool. None of this is necessary if there is a more elegant solution. Anyway, you should never change a system process when you’re not quite sure what you doing, or when there is a simpler and better solution for the problem.
My approach begins with the command-line tool named
launchctl, which is included in OS X by default. It’s not necessary to kill or patch the Remote Control Daemon but it’s only necessary to simply turn the RCD off. That’s possible with
$ launchctl unload -w /System/Library/LaunchAgents/com.apple.rcd.plist
After that terminate iTunes and Spotify and your own application can access the Media Keys without iTunes is restarted again. For this purpose I wrote a simple Mac application which catches all Media Key inputs and forwards them through
cmus-remote to cmus. Of course it’s necessary that the Cmus Control Daemon is running in the background, just like with iTunes or Spotify. But the Cmus Control Daemon has the advantage that it runs only in the background and doesn’t require windows. For this purpose the daemon is automatically registered with
launchctl and launched thereafter. Further informations can be found on the Cmus Control GitHub project page.
-  superuser.com: How can I remap a play-button keypress from a Bluetooth headset on OS X?
-  lifehacker.com: Free Your Mac’s Media Keys from iTunes, No Manual Hacking Required
-  gavinroy.com: Disabling default iTunes behavior with media keys in OSX