Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you listen for Mediakey events under gnome 3 using python?

I'm trying to listen for MediaKey events under Gnome 3 (Gnome Shell). All the examples I find refer to using DBus to connect to org.gnome.SettingsDaemon.MediaKeys. This service doesn't seem to exist on my platform.

I'm trying to do this using Python via GObject-Introspection. The examples say do something like this

from gi.reposiotry import Gio

connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
proxy = Gio.DBusProxy.new_sync(connection, 0, None, 'org.gnome.SettingsDaemon', '/org/gnome/SettingsDaemon/MediaKeys', 'org.gnome.SettingsDaemon.MediaKeys', None)

This fails, unsurprisingly. Am I missing an install which provides this service, or do I have to do this another way?

UPDATE

This is for a media key listener, which listens for key events no matter which window has the focus. It's meant for an app which doesn't even have it's own GUI, and is Desktop wide. That's why I tried the Mediakeys DBus service, only to find it is missing from my Desktop.

UPDATE 2

I should be clear, the MediaKeys service is not present. I can't event connect to the service and create the proxy as it's not there. What I'm wanting to know is, am I missing an install, or was this service removed in one of the Gnome 3 updates? If it was removed, then how can I listen for Media Keys in this new environment? I'm running Gnome 3.8.2.

UPDATE 3

Sorry should have mentioned this in the first place. I'll perfect my question asking one day :-}. I'm running Gentoo.

like image 905
James Hurford Avatar asked Sep 24 '13 12:09

James Hurford


1 Answers

Have you actually seen this question? can't get dbus signal listener to work in C with gnome multimedia keys

The questioner said this code works:

#!/usr/bin/env python
"""Printing out gnome multi media keys via dbus-python.
"""
import gobject
import dbus
import dbus.service
import dbus.mainloop.glib


def on_mediakey(comes_from, what):
    """ gets called when multimedia keys are pressed down.
    """
    print ('comes from:%s  what:%s') % (comes_from, what)
    if what in ['Stop','Play','Next','Previous']:
        print ('Got a multimedia key!')
    else:
        print ('Got a multimedia key...')

# set up the glib main loop.
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
bus_object = bus.get_object('org.gnome.SettingsDaemon', 
                            '/org/gnome/SettingsDaemon/MediaKeys')

# this is what gives us the multi media keys.
dbus_interface='org.gnome.SettingsDaemon.MediaKeys'
bus_object.GrabMediaPlayerKeys("MyMultimediaThingy", 0, 
                               dbus_interface=dbus_interface)

# connect_to_signal registers our callback function.
bus_object.connect_to_signal('MediaPlayerKeyPressed', 
                             on_mediakey)

# and we start the main loop.
mainloop = gobject.MainLoop()
mainloop.run()

Update:

It seems that your problem is with your Gnome distribution, as someone else had encountered previously in this bug report. So probably you should upgrade your distribution.

like image 155
justhalf Avatar answered Nov 07 '22 08:11

justhalf