Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

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

share|improve this answer
    
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
2  
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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