Volume Control With XMonad

Chris Siebenmann’s blog is among my favorites. Frequently, after reading an entry, I am overcome by the urge to investigate and fix some long-standing nuisance.

In the linked posting, he describes wiring up keyboard-volume-key press events to the Linux mixer. After having used a Mac some for work, it slowly dawned on me that keeping alsamixer running in the background on a terminal was a ramshackle thing. If I wanted to show someone a video at my desk, I’d find myself hunting around for the mixer, firing up pavucontrol to send audio over HDMI to the monitor’s speakers. The audience was probably eager to get on to something else.

My workstation runs X with XMonad as the window manager, but no Gnome or KDE or any such thing, and apathy prevented me from ever figuring out whether keyboards sent standard volume up/down events and how I might get ALSA or Pulse to do something with them

It couldn’t be much simpler. To your *~/.xmonad/xmonad.hs, add:

import Graphics.X11.ExtraTypes.XF86

...

, modMask = mod4Mask
} `additionalKeys`
[ ((mod4Mask, xK_Scroll_Lock), spawn "xscreensaver-command -lock")
, ((mod4Mask, xK_Print), spawn "xscreensaver-command -lock")
, ((mod4Mask, xK_x), shellPrompt myXPConfig)
, ((0, xF86XK_AudioRaiseVolume), spawn "pactl set-sink-volume 0 +1.5%")
, ((0, xF86XK_AudioLowerVolume), spawn "pactl set-sink-volume 0 -- -1.5%")
, ((0, xF86XK_AudioMute), spawn "pactl set-sink-mute 0 toggle")
]

Ignore my xscreensaver keys unless you’ve been looking for something like that, in which case, there you have them.

If you’re also an xmobar user and you’d like to have a widget to show the volume level, have a look at https://github.com/bchurchill/xmonad-pulsevolume.git. He’s added a script that wraps the pactl commands; you should have no trouble figuring out how to hook that up if you got this far