Skip to content
Fragmented Development

Manually adjust pulseaudio volume with media keys

One of my favorite things about keyboards in the past few decades (and I really like keyboards) are media keys. Play, pause, mute... and most important, the raise and lower volume keys. Tying these functions to my keyboard make it simple and quick to change what's happening with my computer's sounds.

Usually these are set to sensible default behaviors in most Linux distributions, but I tend to roll my own desktops. When you're gluing together your own window manager, applications, and sessions, you don't get much for free! I had to set up my own key bindings for media magic, and while most media key shortcuts are handled by the applications making the sounds (or a middle man like audtool), volume is not.

Here are the commands I used to raise, lower, and mute my volume with Pulseaudio:

pactl set-sink-volume @DEFAULT_SINK@ +5%  # Increase sound volume by 5%
pactl set-sink-volume @DEFAULT_SINK@ -5%  # Decrease sound volume
pactl set-sink-mute @DEFAULT_SINK@ toggle # Mute/unmute all sound

The value @DEFAULT_SINK@ picks whatever sound device is set to default, which means that it will switch to headphones or whatever new sound device you plug in, and switch back when it is removed.

Here is the full configuration lines for the i3/sway window managers:

bindsym XF86AudioRaiseVolume exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +5%
bindsym XF86AudioLowerVolume exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -5%
bindsym XF86AudioMute exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle

Tags: linux


Add Your Comment