MacRuby: Detect Keyboard Media Keys
If you’re writing any sort of media application, you’ll likely want to detect and act on keyboard events such as the pressing of the Media Keys.
Responding to the play/pause, next, and previous media keys is actually quite simple. Like most things it is just a matter of knowing what you’re doing. I found an informational thread on Stackoverflow which provided the principle actions needed, but it took me a while to get all the details ironed out.
First we need to subclass NSApplication and override the sendEvent method. I then simply use the NSNotificationCenter to message the application that the key was pressed.
You’ve got your fancy new class, but now what? Your plist file is under the Supporting Files directory of your Xcode project. Simply change the
Principal classto the name of your class, in this example
MyApplication.
Shablagoo!
