I wanted to create keyboard and mouse bindings for PulseAudio volume control, which was slightly more complicated than I thought it’d be. The following is a simple script, written in fish shell, that can be used to adjust PulseAudio volumes via the command line. It can be used in conjunction with
xbindkeys to create both keyboard and mouse shortcuts for controlling volume and music.
PulseAudio has several different audio sinks. I often use Bluetooth headphones. So when I issue a volume commands, I need to ensure it adjusts the volume on the correct device. In the following example, we use the
pactl command to both get the current status of PulseAudio devices, and set the volume of the default sync.
#!/bin/fish switch "$argv" case up set type 'volume' set cmd '+10%' case down set type 'volume' set cmd '-10%' case mute set type 'mute' set cmd 'toggle' case '*' echo 'Usage: pavol [up|down|mute]' exit 2 end set default (pactl info | grep "Default Sink" | cut -f2 -d: | sed 's/^ *//') pactl set-sink-$type $default $cmd
xbindkeys, this volume control script can me mapped to hotkeys. If you have a newer mouse filled with tons of seemingly useless buttons, you can easily map your mouse buttons too. Use the
xev command to determine each of your mouse’s button numbers. The following
~/.xbindkeysrc configuration is for mapping
mpc commands (the command line tool for the Music Player Daemon) and the above volume control script with a Mad Catz M.O.U.S.9.
"/path/to/your/scripts/pavol up" b:13 "/path/to/your/scripts/pavol down" b:14 "mpc toggle" b:11 "mpc random" b:10 "mpc next" b:7 "mpc prev" b:6
You can use
xbindkeys for keyboard bindings as well. I use the i3 tiling window manager and use its configuration to map volume control to specific media keys. In the following example, I map my laptop’s volume control keys in my
... bindsym XF86AudioRaiseVolume exec /home/cassius/sumx/bin/pavol up bindsym XF86AudioLowerVolume exec /home/cassius/sumx/bin/pavol down bindsym XF86AudioMute exec exec /home/cassius/sumx/bin/pavol mute ...
I originally wanted to place all my key bindings in my i3 configuration, but at the time there was limited support for mouse support. Currently there is more mouse support, but it’s geared more towards using a mouse for i3 events (e.g. resizing and moving windows). Depending on your setup, you may be able to add these shortcuts into your desktop environment’s hotkey configuration.