So I bought this keyboard for my laptop that doesn't have media keys on it and I am very used to pressing the play/pause button on my laptop to pause any media playing in the background. With this keyboard however I have to create my own keyboard shortcuts to do that because of the absence of media keys. Till now I have managed to make keyboard shortcuts for volume control.

Research and my attempts:

  1. I found this page which shows a command to simulate media keys from the terminal. https://askubuntu.com/questions/235126/simulate-media-keys-in-terminal/235181#235181. The command is xdotool key XF86AudioPlay but when I make a keyboard shortcut out of this by going to Settings > Keyboard > Application Shortcuts. It doesn't seem to work.

  2. When I press the keyboard shortcut which I have set to Super+F11 my CPU usage rises a bit which means that the shortcut is working. I confirmed this by changing the command the shortcuts executes to exo-open --launch WebBrowser which as you might have thought opens my web browser when I press Super+F11.

  3. Now because I knew a bit of C/C++ I created this wrapper program which does exactly the same thing

#include <stdlib.h>

int main() {
    system("xdotool key XF86AudioPlay");

and mapped the shortcut to execute this program but Alas!! it still doesn't work.

How do I get the command that XFCE executes when I press the default play/pause button on my laptop keyboard because that seems to play/pause any media playing in the background.

I use XFCE on Arch Linux. I usually listen to music on rhytmbox music play.

  • 1
    It sounds like your XFCE does not currently know that it needs to send an appropriate play/pause command to your media player of choice, when it receives the media key event. Once you know how your media player expects to have those commands sent, you should set your (global) keyboard shortcuts to send those commands instead of trying to simulate media keys which aren't necessarily configured to do the right thing by default. Since you did not identify which media player you're using, it is going to be difficult to offer any more advice than that. – telcoM Aug 10 '20 at 9:44
  • @telcoM I think I have answered your comment in the edit I made to the my question. – Mayank Shigaonker Aug 10 '20 at 11:12
  • You don't need to simulate XF86AudioPlay to get the application to hear your keypresses. What you need is to write a script that will send a command of your media player that controls the play/pause. But we need to know the media player. For example, with cmus, cmus-remote -u is what toggles play/pause. – Quasímodo Aug 10 '20 at 11:30

Rhythmbox seems to support both rhythmbox-client --play/--pause commands, and D-Bus commands sent using dbus-send, as explained in this question on AskUbuntu and this question here on U&L.SE as linked from that AskUbuntu answer.

In short, the dbus-send command would be:

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

with the condition that Rhythmbox's MPRIS API needs to be enabled first.

But if you need to also control any YouTube videos using those same keypresses, you'll probably need those keypresses to activate a script that figures out which player is currently doing the playing, and sends the right commands either to Rhythmbox or to whichever web browser (or other app) you're using to play YouTube videos. And for the browser, the command/keypress would need to be sent for the correct browser tab, which won't necessarily be the current one...

  • I think I got how XFCE does the play/pause. In the xfce4-pulseaudio-plugin there are two options for playing/pausing anything on rhythm box or vlc media player and all it does it toggle between these when I press the key (Yup it doesn't pause youtube videos. Sorry that was a wrong assumption). Now I'll re-create this functionality with a C++ program. Thanks!! – Mayank Shigaonker Aug 10 '20 at 13:16
  • @MayankShigaonker did you happen to make any progress with this project? My laptop doesn't have a play/pause button and I'd love to get a custom shortcut working in XFCE. Thank you and sorry for being cheeky! – Jess Feb 8 at 16:20
  • @Jess sorry I really forgot about this post 😅. I've updated it with the answer I found to the problem. Good Luck :) – Mayank Shigaonker Feb 9 at 18:11
  • @MayankShigaonker thank you for responding! – Jess Feb 10 at 16:42

If you only want to control rhythmbox then rhythmbox-client --play to play the song or rhythmbox-client --pause to pause the song should be enough.

But I found this software called playerctl in some Arch wiki which control the currently playing media. I'm not 100% sure what its priority is but when I have a Youtube video open it controls that by default rather than messing with Rhythmbox. To toggle between play and pause run playrctl play-pause. To go to the next vid/music you run playrctl next and for the previous vid/music you run playrctl previous.

I'm not 100% sure if this works on any other distro or if its even available, but it works for me on Arch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.