How To Get Global Media Shortcuts in KDE

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
NarcT

How To Get Global Media Shortcuts in KDE

Post by NarcT »

One thing that bugged me about KDE was the lack of Global Media Shortcuts with music/video players. If you want a key for the Play/Pause, Next, Previous, or Stop functions you might have noticed they are assigned to Amarok by default. Well I wanted to be able to pause my music no matter if it was playing through Amarok, Banshee, Pithos, or any other player.

One caveat at the moment, this only works with applications that support MPRIS or "Media Player Remote Interfacing Specification". A large amount of media applications already support this but if you aren't sure open your application and type this code in a terminal.

Code: Select all

qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'
If you see your apps name after typing this command into the terminal then it will work. Make sure your application is open before you run this command.

[Overview]
The overview of my fix:
Say I want to be able to set the Play/Pause button to work on any media player allowing me to play or pause the music. First create a script to send the the Play/Pause command to your applications.

Here's my script for Play/Pause:

Code: Select all

#!/bin/bash

## -- This script will imitate Gnome's Media Controls (Play/Pause, Next, Previous, Stop) -- ##
## -- It will assume you are using a media application that is compatible with MPRIS or  -- ##
## -- "Media Player Remote Interfacing Specification"                                    -- ##

# Search for running media applications
# and store into apps array
apps=(`qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'`)

# For each application send the "PlayPause" command
for app in "${apps[@]}"
  do    
        qdbus org.mpris.MediaPlayer2.$app /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  done
If you notice the actual command is specified at the end of the 2nd to last line. This "PlayPause" instructs the application to do what we want. But what if we want to add a Next Song key? Just copy this script and change the "PlayPause" to "Next". Pretty simple.

Now all we have to do is assign this script to a global shortcut. In KDE go to System Settings > Shortcuts and Gestures > Custom Shortcuts. Right click in the white space and select New > Global Shortcut > Command URL. Name your Action whatever you like and now click the Trigger tab and assign your appropiate shorcut. Now click the Action tab and browse to your previously made script. Make sure your script is executable by quickly typing "chmod +x /home/USER/script". After you click Apply you should be able to control all supported media applications with your shortcut!
digitsm

Re: How To Get Global Media Shortcuts in KDE

Post by digitsm »

Yeees! That worked for me even in Xfce. With this method you can Play/Pause/Next/Previous your music in EVERY desktop environment and with EVERY music player.
However the final bash script I used was a little different with NarcT's, as follows:

Code: Select all

#!/bin/sh
## -- This script will imitate Gnome's Media Controls (Play/Pause, Next, Previous, Stop) -- ##
## -- It will assume you are using a media application that is compatible with MPRIS or  -- ##
## -- "Media Player Remote Interfacing Specification"                                    -- ##

# Search for running media applications
# and store into apps array
# *I used NO parentheses
apps=`qdbus | grep org.mpris.MediaPlayer2 | sed 's/.*\.//'`

# For each application send the "PlayPause" command
# *I used apps instead of apps[@]
for app in "${apps}"
  do    
        qdbus org.mpris.MediaPlayer2.$app /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  done
The only problem is that I had to use qdbus which is based on Qt. Because Xfce uses Gtk+2 this eliminates the purity of Xfce. (I couldn't find an equivalent for qdbus in Xfce)
Post Reply

Return to “Tutorials”