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:
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.
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.
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.
dbus commands for controlling Spotify are:
u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
u@pc:~$ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
This shouldn't even need a description, but unfortunately the dreadful keyboard shortcut UI makes this a mission:
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:
Find the name of the media key you wish to bind to using
xmodmap -pk and
sudo showkey. I ended up with
'XF86AudioPrev' for my three keys.
Go into the UI & add the custom shortcut but don't specify the key.
Find the entry for the custom shortcut in the DConf database using
u@pc:~$ gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ name
Once you've found your custom shortcut, modify the binding via:
u@pc:~$ gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/ binding 'NEW_KEY_NAME'
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.
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.
© Ben Talbot. All rights reserved.