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
[1] and much less patching it with a shady tool[2]. 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.
Solution
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[3]. That’s possible with launchctl
:
$ 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.
Footnotes
- [1] superuser.com: How can I remap a play-button keypress from a Bluetooth headset on OS X?
- [2] lifehacker.com: Free Your Mac’s Media Keys from iTunes, No Manual Hacking Required
- [3] gavinroy.com: Disabling default iTunes behavior with media keys in OSX