Last active
December 16, 2015 16:19
-
-
Save tanelpuhu/5461979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from dbus.mainloop.glib import DBusGMainLoop | |
import subprocess | |
import gobject | |
import dbus | |
appName = 'Spotify Media Keys' | |
def handleMediaKey(app, *keys): | |
if app != appName: | |
return | |
method = None | |
for key in keys: | |
if key == 'Play': | |
method = 'PlayPause' | |
elif key in ['Stop', 'Next', 'Previous']: | |
method = key | |
if method: | |
subprocess.call([ | |
'dbus-send', | |
'--print-reply', | |
'--dest=org.mpris.MediaPlayer2.spotify', | |
'/org/mpris/MediaPlayer2', | |
'org.mpris.MediaPlayer2.Player.' + method | |
]) | |
if __name__ == '__main__': | |
DBusGMainLoop(set_as_default=True) | |
bus = dbus.Bus(dbus.Bus.TYPE_SESSION) | |
mediaKeys = bus.get_object('org.gnome.SettingsDaemon', '/org/gnome/SettingsDaemon/MediaKeys') | |
mediaKeys.GrabMediaPlayerKeys(appName, 0, dbus_interface='org.gnome.SettingsDaemon.MediaKeys') | |
mediaKeys.connect_to_signal('MediaPlayerKeyPressed', handleMediaKey) | |
gobject.MainLoop().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment