25

I have a new android phone that has a number of apps for communicating though an ssh connection. I would like to know if I can use ssh commands if I can simulate key pressed, specifically the media keys (play pause volume). I would prefer not to install additional software.

Note: I know there are apps that do this out of the box, this is simply for a better understanding.

44

The answer is xdotool, and it might already be installed on your Ubuntu system. It can simulate keypresses. See also [1].

Some commands:

  • play/pause

    xdotool key XF86AudioPlay
    
  • previous/next

    xdotool key XF86AudioPrev 
    xdotool key XF86AudioNext
    
  • volume down/up

    xdotool key XF86AudioLowerVolume 
    xdotool key XF86AudioRaiseVolume
    
  • mute

    xdotool key XF86AudioMute
    

For more XF86 commands see [2].

Instead of doing the volume step by step you can also set it using alsamixer or pulseaudio (recommended). Assuming you use device 0 (you can check this with alsamixer or pacmd list-sinks) you can set the volume to for instance 80% with

  • alsamixer (-c 0 specifies the sound device id)

    amixer -c 0 sset Master,0 80%
    
  • pulseaudio (the 0 in front of the 80% is the sound device id)

    pactl set-sink-volume 0 80%
    

For more information about these commands see their manpages.


EDIT: If you're trying to do the xdotool commands over ssh and get the following error message

Error: Can't open display: (null)
Segmentation fault

you need to set the DISPLAY variable:

export DISPLAY=':0.0'

After that it should work without problems


[1] http://www.semicomplete.com/projects/xdotool/xdotool.xhtml
[2] http://wiki.linuxquestions.org/wiki/XF86_keyboard_symbols

  • What is required for this to work over SSH? Using these commands in a remote shell results in simulation on local machine vs remote machine. – earthmeLon Jan 5 '13 at 3:14
  • 3
    @earthmeLon Are you sure you are running the command on the remote machine? Because for me it works fine over ssh. I needed to set the DISPLAY variable though, see my edit. – Gerhard Burger Jan 5 '13 at 10:29
  • Setting the display as you suggested as resolved the problem. Thanks :D – earthmeLon Jan 6 '13 at 0:51
  • 2
    Note that when you are binding these to some keys in xfce4, you'll need to use the --clearmodifiers flag, something like this: xdotool key --clearmodifiers XF86AudioPlay – cab404 Dec 19 '16 at 19:52
  • What @cab4040 mentioned also applies to GNOME. If one wishes to add custom media keys on a keyboard that lacks them, one could go to the Keyboard Shortcuts section in settings and add a binding between a keyboard shortcut and an xdotool key command with --clearmodifiers, see above. – Pierre Thalamy Mar 28 at 12:58

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.