40

I'm running a pretty-much brand-new Ubuntu 17.10 installation on a Thinkpad T470s.

Whenever I'm hitting one of the media keys (play/pause, skip, etc) on any one of my keyboards or bluetooth-headset I'm seeing this icon:

popup on media key indicating no action allowed

flash on the monitor but none of my media players react.

The keys are recognized in showkey:

~$ sudo showkey -k
press any key (program terminates 10s after last keypress)...
keycode 163 press
keycode 163 release
keycode 165 press
keycode 165 release
keycode 164 press
keycode 164 release

But they don't show up in xev.

k0pernikus
  • 6,336

9 Answers9

40

This whole issue looks like an "it's not a bug, it's a FEATURE" present from Gnome.

In short: when you press a media button it generates a keycode which is then translated into a command. Let's say you press a Play/Pause button. It generates a keycode 162 and a command XF86AudioPlay.

Now almost every media application that may be waiting for this event (be it VLC, totem, kodi, spotify etc.) expect to receive pure XF86AudioPlay command. And what Gnome does? It intercepts this command and translate it into it's own command "play". Because of this neither xev nor xbindkeys show this event properly - they never receive a command they can understand.

When you press a media button Gnome receives the command and checks if there is any app that its recognized as capable of receiving this command. If there is (let's say totem, rhytmbox, maybe VLC) it sends "play" that should work. If there is no app recognised as capable of receiving this command Gnome will show the sign that is attached to first post and won't send any command anywhere.

The solution is simple - make Gnome unable to intercept media key events. Install dconf-editor , go to org.gnome.settings-daemon.plugins.media-keys and change any button that should work from XF86SomeExample (e.g., XF86AudioPlay) to none (''). This way any app should receive key command directly.

cxw
  • 107
  • 5
darkdude
  • 401
  • 4
  • 3
37

If the keyboard media keys do not work from your Ubuntu desktop, you can use D-Bus support to send the proper commands to Spotify. Validate the following commands from the console:

Play/Pause

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

Next

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next

Previous

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

Enable Media Key Shortcut

To tie these to the keyboard, go to Settings > Keyboard > Shortcuts > Custom Shortcuts, then “Add” with +. Use one of the command above, then hit the key that you want to tie to the new command.

enter image description here

Source

https://fabianlee.org/2016/05/25/ubuntu-enabling-media-keys-for-spotify/

totymedli
  • 504
21

For some this may be a browser-related issue for instance (Google Chrome) "holds" the media keys. Even without browser media, the keys are allocated to Chrome.

Solution:

  1. Paste chrome://flags/#hardware-media-key-handling at Chrome;
  2. Select "Disabled" for "Hardware Media Key Handling" - it's the first option;
  3. Chrome should ask to "relaunch", accept and proceed to enjoy apps!
11

I found that, with Ubuntu 18.04 LTS as of 2019/2/1, all I needed to do was:

  • install dconf-editor
  • navigate to org/gnome/settings-daemon/plugins/media-keys
  • for each key not working:
    • uncheck "Use default value"
    • hit apply
    • re-check "Use default value"
    • hit apply again

Media keys in question should now work immediately.

Using no value, as per darkdude recommended, didn't work for me.

4

combination of last two answers works for me. I have Ubuntu 18.04 the keyboard test work

~$ sudo showkey -k
press any key (program terminates 10s after last keypress)...
keycode 163 press
keycode 163 release
keycode 165 press
keycode 165 release
keycode 164 press
keycode 164 release

but when I go to Activities -> Keyboard and try to do anything with Play / Next / Previous it not work at all. When I deactivated default key association (invoke dialog for key association, press backspace and click save) and create brand new one it works, very helpful was answer from @foamboarder

now it looks like this screenshot from shortcut setup all works fine now even after wake up the computer from sleep mode

1

Using the above answer by Václav Rak worked for me on Ubuntu 18.04, in particular

... deactivated default key association (invoke dialog for key association, press backspace and click save) and create brand new one.

I use Rhythmbox. Here are the commands I was putting for each Custom Shorcut for Rhythmbox:

For Previous:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.rhythmbox /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

For Next:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.rhythmbox /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next

For Play/Stop:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.rhythmbox /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

Hope this helps someone one day

Zanna
  • 72,312
1

TLDR: A bug fixed in Eoan (19.10) should address these issues.

There was a bug in gnome-settings-daemon that was corrected in version 3.32.1. Ubuntu 19+ uses this version or higher, so anyone running 18 or below is affected, unless they patch GSD.

Follow the steps in this comment: https://gitlab.gnome.org/GNOME/gnome-settings-daemon/-/issues/45#note_444348

The diff can be obtained here: https://gitlab.gnome.org/GNOME/gnome-settings-daemon/-/commit/cea632bf682760e80654df19cfef6206efca868a.diff

I patched 3.28.1 (latest in 18.04.4 LTS, as of this message) and media keys are working much better.

If you feel confident enough to patch, I would suggest it! Or upgrade to eoan.

Once running a fixed version, you shouldn't need to muck around with reassigning media keys and this will return to being a "Feature."

0

The key-codes seem not to be mapped accordingly. You can manually map keys as described in the ubuntu documentation: https://help.ubuntu.com/stable/ubuntu-help/keyboard-shortcuts-set.html

  1. Open the Activities overview and start typing Keyboard.
  2. Click on Keyboard to open the panel.
  3. Select the Shortcuts tab.
  4. Select a category in the left pane, and the row for the desired action on the right. The current shortcut definition will change to New accelerator…
  5. Hold down the desired key combination, or press Backspace to clear.
Arne Q
  • 26
0

On 22.04, I tried clearing and restoring defaults in dconf-editor, disabling media key handling (which had to be reversed), and trying to manually bind keyboard shortcuts in Activites -> Keyboard, all with no luck.

What finally worked for me was this answer from a similar question, which uses xbindkeys to re-bind the media buttons to playerctl commands.