dwm — Adding Keyboard Special Function Keys ie. Brightness & Volume

Daniel Jordan Osborn
4 min readApr 14, 2021

If you are using dwm as your windows manager and want to use your special keyboard keys, controlled from dwm via `config.def.h`, then you will need to do a few things.

Random shot of config.def.h

dwm(Dynamic Window Manager) is a window manager for X11. If you want to learn more about it, here is its home page at suckless, and here is some more help at Arch Wiki. You should learn how to build dwm and some basics before adding this. There are also other ways to make this happen. You could use a power manager like xfce4-power-manager and let it control brightness. Also volumeicon can control volume keys. But if you like making things complicated or learning random stuff then continue.

the dwm logo from suckless.org
dwm logo form suckless.org

You will need to import XF86keysym.h into your config file at the very top.

config.def.h

#include <X11/XF86keysym.h>

This will give you access to a few new variables that link to the special keys on your keyboard.

To see what is available you can look here.

Examples:

To add brightness keys and volume keys:

#include <X11/XF86keysym.h>
/* Variables */
/* Appearance */
/* Tagging */
/* Rules */
/* Layout(s) */
/* Key Definitions */
/* Commands */
// Brightness Commands
static const char *brightness[2][] = {{"xbacklight", "-inc", "5%", NULL},{"xbacklight", "-dec", "5%", NULL}};
// The rest of you commands..../* Keys */
static Key keys[] = {
/* modifier key function argument */
{0, XF86XK_MonBrightnessUp, spawn, {.v=brightness[0]},
{0, XF86XK_MonBrightnessDown, spawn, {.v=brightness[1]},
// The rest of your keys and config file......

Above in /*Commands */ , we put what we want to happen when the buttons are pressed. You could anything but in the example, I use xbacklight the command-line tool to change the brightness up or down. To learn more about what backlight can do click here. You may need to install it or depending on your hardware use a different command.

In the /* Keys */ , we put what key, and what it does when pressed. It takes four inputs…

  1. Modifier
  • Here you would put if you want to use a Mod key like shift or alt. Because we are not using any mods here we use 0.

2. Key

  • What key you will press, normal keys start with XK_, these news ones start with XF86XK_, to see the list of them here.

3. Function

  • This says how we will use the next argument. spawn runs it like a normal command. There are special things here you can do like zoom or window size.

4. Argument

  • This is an array that return a value and the above says what to do with it . Here we want to execute xbacklight and increase or decrease the brightness. We do this by calling our variable we made called brightness, then we say run brightness[0] for up or [1] for down.

You can do the same for Volume…

#include <X11/XF86keysym.h>
/* Variables */
/* Appearance */
/* Tagging */
/* Rules */
/* Layout(s) */
/* Key Definitions */
/* Commands */
// Volume Commands
static const char *volume[3][4] = { {
"pactl", "set-sink-volume", "@DEFAULT_SINK@", "+10%"
},{
"pactl", "set-sink-volume", "@DEFAULT_SINK@", "-10%"
},{
"pactl", "set-sink-mute", "@DEFAULT_SINK@", "toggle"
}
};
// The rest of you commands..../* Keys */
static Key keys[] = {
/* modifier key function argument */
{0, XF86XK_AudioRaiseVolume, spawn, {.v=volume[0]},
{0, XF86XK_AudioLowerVolume, spawn, {.v=volume[1]},
{0, XF86XK_AudioMute, spawn, {.v=volume[2]},
// The rest of your keys and config file......

This example will not work for everyone. Here I am using commands that will work with Pulseaudio. In my config, I use an executable script, /usr/local/bin/volume . I call it when the buttons are pressed and do the commands from there, that way I can make changes without rebuilding or restarting dwm.

If you have trouble with xbacklight and others you can try this script, make it executable and place it in your path like /usr/local/bin/brightness_push Then you can call it like above example but use, brightness_push up and, brightness_push down .

You can also not use XF86XK keysyms and just use ACPI. It is pretty simple to set up. ACPI has event handlers that listen for button presses, even the power button, and laptop lid. So you can do some pretty funky stuff if you mess it up but it is fun.

Please let me know if I have messed something up on here or you have trouble. Hope it helps

--

--

Daniel Jordan Osborn

I can't find anything, all I do is look and look. By the time I found that one thing, that other thing I was holding, it's lost... :wq