Skip to content

Instantly share code, notes, and snippets.

@adamv
Last active May 20, 2016 15:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamv/b2a0aee9ac04229a4b93a0c09999f27c to your computer and use it in GitHub Desktop.
Save adamv/b2a0aee9ac04229a4b93a0c09999f27c to your computer and use it in GitHub Desktop.
#!/bin/bash
# kill the media key program on exit
trap 'kill $(jobs -p)' EXIT
# prevent iTunes from capturing media keys
launchctl unload -w /System/Library/LaunchAgents/com.apple.rcd.plist 2>/dev/null
# launch the media key program in the background
(cmus-media-keys) &
# pipe CoreAudio messages to null:
# https://github.com/cmus/cmus/issues/421
$(brew --prefix cmus)/bin/cmus "$@" 2>/dev/null
#!/usr/bin/python
# Use the system Python, as it has the Objective-C bridge
# Derived from: https://gist.github.com/drslump/2870240
import subprocess
from AppKit import NSApplication, NSSystemDefined, NSApplicationActivationPolicyProhibited
from PyObjCTools import AppHelper
KEY_UP = 11
class KeySocketApp(NSApplication):
repeated = False
def sendEvent_(self, event):
if event.type() == NSSystemDefined and event.subtype() == 8:
data = event.data1()
keyCode = (data & 0xFFFF0000) >> 16
keyFlags = (data & 0x0000FFFF)
keyState = (keyFlags & 0xFF00) >> 8
keyRepeat = keyFlags & 0x1
if keyRepeat and keyState != KEY_UP:
if keyCode == 20:
self.repeated = True
# print "prev"
subprocess.call(['cmus-remote', '-k', '-10'])
elif keyCode == 19:
self.repeated = True
# print "forward"
subprocess.call(['cmus-remote', '-k', '+10'])
if keyState == KEY_UP:
if self.repeated:
self.repeated = False
elif keyCode == 20:
# print "PREV"
subprocess.call(['cmus-remote', '-r'])
elif keyCode == 16:
# print "PLAY"
subprocess.call(['cmus-remote', '-u'])
elif keyCode == 19:
# print "FORWARD"
subprocess.call(['cmus-remote', '-n'])
if __name__ == '__main__':
app = KeySocketApp.sharedApplication()
# prevent dock icon
app.setActivationPolicy_(NSApplicationActivationPolicyProhibited)
AppHelper.runEventLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment