I've been plagued by issues with my media keys since moving to Ubuntu 18.04. 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 getting around the difficulties GNOME throws your way.
GNOME intercepts most keys from the XServer, and will redirect them elsewhere when they have a shortcut set. This converts them into something that other programs can't handle apparently. The key conclusion: grabbing keyboard input outside of GNOME was going to be much more effort than working within GNOME.
The easiest solution was to let GNOME continue intercepting media keys, then redirect them to a commands controlling Spotify. Obviously the downside is media keys end up locked to a single application, but that's still better than what we currently have.
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
Next, the commands had to be tied to the media keys using GNOME. This shouldn't need a description, but unfortunately the troublesome keyboard shortcut UI below 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 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 play, next, and previous needing
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 #4 again to get it to stick...
One last thing I didn't get to figuring out, was why the numbers were 8 off what
sudo showkey was displaying. For example key 200 in
showkey corresponded to key 208 in
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, or found it valuable, using the buttons below.
© Ben Talbot. All rights reserved.