Ben Talbot home

Getting working media keys in Ubuntu 18.04+

3 June 2020
Ben Talbot

Since moving to Ubuntu 18.04 I've encountered issues with my media keys. Sometimes they work, sometimes they work in odd ways, and other times they don't seem to work at all.

From what I gather, it seems the first application you open post-boot with media key support calls "dibs" on receiving those events. Which works well for that first application, while it's alive. Open a Chrome tab that grabs the keys first, and as soon you close the tab your media key events will be sent to nothing until you reboot. Even opening Spotify first, closing it, and re-opening later leads to an unreliable experience at best.

Ideally there would be a cute UI somewhere in Gnome that lets you pick the target of your media key events, like maybe here:

A nice place for the "pick media key events target" option

I wasn't able to find any hints on picking the target for media key events in Gnome, so instead I resorted to reliably forcing the events to somewhere useful: Spotify. What follows is a short brain dump of how I got there, with some notes on how to get around the difficulties Gnome throws your way.

 Controlling the target command for media keys

Gnome intercepts certain keys from the XServer, and redirects them elsewhere if they have a shortcut set. This process converts them into something that other programs can't handle apparently. I didn't verify this mangling, but I did confirm the keys are definitely intercepted when a Gnome shortcut requires the key.

 Controlling Spotify playback with commands

The easiest solution for me was to let Gnome continue intercepting media keys, but simply redirect them to a command that directly controls Spotify. Obviously the downside is that your media keys are locked to a single application, but that's better than what we currently have.

The dbus commands for controlling Spotify are:

  • play / pause
    u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  • next
    u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
  • previous
    u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
 Linking the media keys to a command

This shouldn't even need a description, but unfortunately the dreadful keyboard shortcut UI makes this a mission:

The Gnome custom keyboard shortcut UI...

The UI looks cute, but that's where the utility ends. It only allows you to physically enter the keys, and doesn't register most special keys, including our media keys. So instead of simply using of the UI, we embark on this journey:

  1. Find the name of the media key you wish to bind to using xmodmap -pk and sudo showkey. I ended up with 'AudioPlay', 'XF86AudioNext', and 'XF86AudioPrev' for my three keys.

  2. Go into the UI & add the custom shortcut but don't specify the key.

  3. Find the entry for the custom shortcut in the DConf database using gsettings:

    u@pc:~$ gsettings get name
  4. Once you've found your custom shortcut, modify the binding via:

    u@pc:~$ gsettings set binding 'NEW_KEY_NAME'
  5. If there is a previous shortcut using that key, you next need to edit your shortcut (change a character or whatever) in the UI, & then click replace to force disable of the previous shortcut.

  6. Then you seem to have to do step #3 again to get it to stick...

Note: I couldn't figure out why the numbers were 8 off what sudo showkey was displaying (e.g. key 200 in showkey == key 208 in xmodmap), but this will get you there nonetheless.

And there we have it, usable media keys in Ubuntu 18.04+. I don't know why it's this messy, or if this is the cleanest hack, but it was enough to get me somewhere. As always, I'd be interested in a proper solution if anyone has any tips!

Let me know below if you liked this content, found it valuable, or have any comments.

Like this?

Loading ...

© Ben Talbot. All rights reserved.