Multimedia hotkeys on a plain GUI

Most desktop environments already have a daemon to deal with so-called multimedia hotkeys, but I use plain Openbox, hence the clunky title.

Preparation

Test your setup with xev1:

  • launch the command in a terminal
  • make sure the little window stays focused
  • press keys and watch the terminal output, look for things like
    KeyPress event (...) keycode XX (keysym 0xXXXX, <KeyName>) (...)
    (take note of <KeyName> and/or keycode XX)

Multimedia keys are not just on your keyboard. Also try wired or wireless peripherals - my bluetooth speaker sends the appropriate key presses when I press its Play/Pause button.
The system just doesn't pick them up - yet - that's what this article is about.

I found this great tutorial and adapted it slightly to my own situation.

playerctl

The main actor is the playerctl application. It's in the Arch repositories and probably most other distros', too.

Just install it; it's not a daemon, it simply serves as a command line tool to send the appropriate command to any app that has registered itself on the MPRIS Dbus interface.

Please refer to this chapter of the original tutorial for a quick introduction.

Here's a list of available commands (man playerctl):

  • status: Get the current status of the player.
  • play: Command the player to play.
  • pause: Command the player to pause.
  • play-pause: Command the player to toggle between play and pause.
  • stop: Command the player to stop.
  • next: Command the player to skip to the next track.
  • previous: Command the player to skip to the previous track.
  • position [OFFSET[+|-]]: Print the position of the current track in seconds. With OFFSET specified, seek to OFFSET seconds from the start of the current track. With the optional [+|-] appended, seek forward or backward OFFSET seconds from the current position.
  • volume [LEVEL[+|-]]: Print the player's volume scaled from 0.0 (0%) to 1.0 (100%). With LEVEL specified, set the player's volume to LEVEL. With the optional [+|-] appended, increase or decrease the player's volume by LEVEL.
  • metadata [KEY]: Print all metadata properties for the current track set by the current player. If KEY is specified only the value of KEY is printed.
  • open URI: Open URI in the player. URI may be the name of a file or an external URL.
  • shuffle [On | Off | Toggle]: Print the shuffle status of the player. With the shuffle status specified, set the shuffle status to either On , Off , or Toggle
  • loop [None | Track | Playlist]: Print the loop status of the player. With the loop status specified, set the loop status to None (disable looping), Track (loop the current track), or Playlist (loop the current playlist).

Most media applications (Firefox, Chromium, VLC...) already support MPRIS, but my two favorites - MPV for video and MPD for audio - do not. Fortunately 3rd party daemons/plugins can provide MPRIS functionality for both.

MPD

Install mpDris2 (AUR package), start & enable the systemd user service.

MPV

Install the mpv-mpris plugin (repo package). MPV will load it by default.

Openbox

Now we can add the playerctl commands we want to use to rc.xml. Example:

xml
... <keybinds> ... <keybind key="C-A-Up XF86AudioRaiseVolume"> <action name="Execute"> <command>playerctl volume .03+</command> </action> </keybind> <keybind key="C-A-Down XF86AudioLowerVolume"> <action name="Execute"> <command>playerctl volume .03-</command> </action> </keybind> <keybind key="C-A-space"> <action name="Execute"> <command>playerctl play-pause</command> </action> </keybind> <keybind key="XF86AudioPlay"> <action name="Execute"> <command>playerctl play</command> </action> </keybind> <keybind key="XF86AudioPause"> <action name="Execute"> <command>playerctl pause</command> </action> </keybind> ... </keybinds> ...

And openbox --reconfigure.


  1. How to do this on wayland? wev exists, that's all I can say.