Simulate media keys in terminal

Solution 1:

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