Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Created May 27, 2011 21:07
Show Gist options
  • Save danielwestendorf/996172 to your computer and use it in GitHub Desktop.
Save danielwestendorf/996172 to your computer and use it in GitHub Desktop.
class MyApplication < NSApplication
def sendEvent(event)
if event.type == NSSystemDefined && event.subtype == 8 #determine if one of the media keys was pressed
keyCode = (event.data1 & 0xFFFF0000) >> 16
keyFlags = event.data1 & 0x0000FFFF
keyState = (keyFlags & 0xFF00) >> 8 == 0xA
mediaKeyPressed(keyCode) if keyState
super(nil) #don't passing anything on to super
else
super(event) #not what we were looking for, pass the event on to super.
end
end
def mediaKeyPressed(keyCode)
if keyCode == 20 #previous track key was pressed
NSNotificationCenter.defaultCenter.postNotificationName('MyApplicationPreviousTrack', object:nil)
elsif keyCode == 19 #next track key was pressed
NSNotificationCenter.defaultCenter.postNotificationName('MyApplicationNextTrack', object:nil)
elsif keyCode == 16 #play/pause key was pressed
NSNotificationCenter.defaultCenter.postNotificationName('MyApplicationPlayToggle', object:nil)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment