How do you make volume keys and mute key work in Xmonad

22

5

I am new to Xmonad (just installed it yesterday), and since I have never used haskell before, I found configuration a little bit confusing for me. I got somewhat made xmobar and trayer work, but I have no idea how might I make multimedia keys to adjust volume. Can anyone help with that?

Additional question: How do you manage your volume in xmonad. Do you use tray icon, or other things like that?

Here is my xmonad configuration:

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.EZConfig(additionalKeys)
import System.IO

main = xmonad =<< statusBar myBar myPP toggleStrutKey myConfig

-- Command to launch the bar
myBar = "xmobar"

-- Custom PP, it determines what is written to the bar
myPP = xmobarPP { ppCurrent = xmobarColor "#429942" "" . wrap "<" ">" }

-- Key bindings to toggle the gap for the bar
toggleStrutKey XConfig {XMonad.modMask = modMask} = (modMask, xK_b)

myConfig = defaultConfig {
    manageHook = manageDocks <+> manageHook defaultConfig,
    layoutHook = avoidStruts $ layoutHook defaultConfig,
    modMask = mod4Mask -- Rebind Mod to windows key
    } `additionalKeys`
    [ ((mod4Mask .|. shiftMask, xK_z), spawn "xscreensaver-command -lock")
    ]

yasar

Posted 2012-02-14T12:01:00.820

Reputation: 427

Answers

24

Use 'xev' and tap the multimedia keys to discover their names. One might be 'XF86XK_AudioMute'. Then look at the contents of '/usr/include/X11/XF86keysym.h' and look for the name. On my system, 'XF86XK_AudioMute' is '0x1008FF12'.

Drop that where you would put a key in your config file. It might look like this:

import XMonad
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Util.EZConfig(additionalKeys)
import System.IO

-CUT-

 } `additionalKeys`
    [ ((mod4Mask .|. shiftMask, xK_z), spawn "xscreensaver-command -lock"),
      ((0                     , 0x1008FF11), spawn "amixer -q sset Master 2%-"),
      ((0                     , 0x1008FF13), spawn "amixer -q sset Master 2%+"),
      ((0                     , 0x1008FF12), spawn "amixer set Master toggle")
    ]

'amixer' will set your volume. The '0' replacing mod4Mask allows you to tap the multimedia key without holding your mod key.

Wallace Gean

Posted 2012-02-14T12:01:00.820

Reputation: 366

1Mute key problem: When I start xev and press mute key, there's nothing to find in the terminal. (Other keys are fine.) I looked up from /usr/include/X11/XF86keysym.h, I found this: #define XF86XK_AudioMute 0x1008FF12 /* Mute sound from the system */ Do you know what's the problem with my mute key? Thank you. – Nick – 2014-12-18T00:42:31.240

Another Mute key problem: I can't find the name for Mute either. (I use ThinkPad.) – Nick – 2014-12-22T14:48:44.123

if the amixer set Master 2- doesn't work for your system, try amixer set Master 2%- – zhenjie – 2013-05-30T19:11:39.030

1When I use xev and press volume keys on my HP laptop I get no keycodes, but showkey command show them as keycode 114 and keycode 115. How do I need to change this XMonad config file to work with my keys? – valentt – 2013-07-03T18:38:54.110

On Ubuntu 13.04 this worked copy/paste without looking up the keys symbols. – Cory Klein – 2013-10-16T21:19:16.343

2If the mute above cannot unmute, try amixer -D pulse set Master toggle instead. – zw324 – 2013-12-26T19:26:03.613

14

See this Graphics.X11.ExtraTypes.XF86 for keys you want and add to your config file:

import Graphics.X11.ExtraTypes.XF86
myKeys conf@(XConfig {XMonad.modMask = modm}) = M.fromList $
[ ...
, ((0, xF86XK_AudioLowerVolume   ), spawn "amixer set Master 2-")
, ((0, xF86XK_AudioRaiseVolume   ), spawn "amixer set Master 2+")
, ((0, xF86XK_AudioMute          ), spawn "amixer set Master toggle")
...]

Richard Huang

Posted 2012-02-14T12:01:00.820

Reputation: 241

I get an error with this config: "xmonad.hs:29:1: parse error (possibly incorrect indentation) Please check the file for errors."

That line is where "[ ..." begins ...

Do you know why this happens? – valentt – 2013-07-03T18:09:19.723

4To use Graphics.X11.ExtraTypes.XF86 is the better solution for readability. – erik – 2013-10-04T09:52:45.247

@valentt The line with the dots is just an example. It should look like that, so instead of the three dots you have your first key defined: [ ((modMask, xK_e ), spawn "dmenu_run") – erik – 2013-10-04T09:57:06.537

9

If you're using pulseaudio, pactl also should work.

, ((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")
]

0 is sink id. pactl list short sinks will show sink list.

pactl stat|grep 'Default Sink' | cut -f2 -d':'

will show current default sink. You can use sink name instead numeric id.

Doulble dash -- tells 'this is not option(like -h), just value' to pactl.

Mait

Posted 2012-02-14T12:01:00.820

Reputation: 191

1Nice! However, the -- didn't work for me on 15.10, just removing it did the trick. Also, there is a special name @DEFAULT_SINK@, so you don't need to fiddle around with device specific numbers/names. – iGEL – 2016-01-08T12:54:03.107

1This is perfect, replacing 0 with @DEFAULT_SINK@ in the above commands! At least for those switching from other DEs -- thanks a lot to you! – Jan D – 2016-03-03T12:05:24.080

This answer has the benefit of going over 100% instead of capping (like amixer caps). The bad thing is that it stops working when you play the music from different sink. – styrofoam fly – 2018-05-06T23:48:38.327

3

If amixer set Master 2- does not work. Try amixer -D pulse set Master 2- instead. Also 2%- and 2%+ will change the volume by 2 percent, which may be easier to use. You can test these commands in the terminal to adjust them to your liking before you put them in you xmonad config file.

Justin Raymond

Posted 2012-02-14T12:01:00.820

Reputation: 161

... -D pulse ... worked for me! also, a -q flag quiets the output – Josh.F – 2017-01-16T19:44:01.383