6

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.

3
  • 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
    Commented Aug 10, 2020 at 9:44
  • @telcoM I think I have answered your comment in the edit I made to the my question. Commented Aug 10, 2020 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
    Commented Aug 10, 2020 at 11:30

4 Answers 4

8

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 playerctl play-pause. To go to the next vid/music you run playerctl next and for the previous vid/music you run playerctl previous.

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

2
  • 1
    This solution works on Linux MInt 20 (which is Ubuntu 20.04 underneath) and it stops/plays only one last active player, no matter a Youtube tab or Rhythmbox, which is kind of convenient. Though it also has -a switch to stop all of the available players.
    – Vaviloff
    Commented Oct 21, 2022 at 8:51
  • Thanks for the tip on playerctl! I was trying to control Parole, and learned that I needed to first enable the MPRIS2 plugin under Tools > Plugins. MPRIS (Media Player Remote Interfacing Specification) is the spec playerctl supports.
    – Johann
    Commented Feb 20, 2023 at 17:38
1

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...

3
  • 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!! Commented Aug 10, 2020 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!
    – Furby
    Commented Feb 8, 2021 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 :) Commented Feb 9, 2021 at 18:11
1

I found the same as you: The command xdotool key XF86AudioPlay works from a shell, but not from a shortcut or application launcher. So I concluded that xdotool only works when executed from an interactive shell. And funny enough, I got it to work in keyboard shortcuts and launcher icons (e.g. on the task bar) with the following command:

bash -i -c 'xdotool key XF86AudioPlay'

As soon as you remove the -i switch, thus making bash non-interactive, it stops working. No idea why this is the case...

The nice thing about the XF86AudioPlay method is, that it also works for pausing Youtube videos playing in the background in Firefox, for example.


Update

Okay, I found out why this is happening. In my .bashrc I had changed $LD_LIBRARY_PATH, which was necessary for xdotool to run. However, this did not happen when logging into my GUI session (XFCE). So any program started directly from the GUI (e.g. a launcher or Alt+F2) and not from a terminal would not see the modified $LD_LIBRARY_PATH, thus missing some libraries.

So the correct solution actually was to put the modifications of $LD_LIBRARY_PATH in .profile instead of .bashrc. That way, XFCE would execute them on login and all programs could see them. Nevertheless I will leave the old answer here for future visitors.

0

In case you're using Audacious, you can use

audacious -t

to toggle between pause and play (see man audacious).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .