Binding keyboard volume buttons to mixer control

Here’s how to bind your specific buttons (such as + and – controls for volume and the mute button) to controlling your mixer in openbox. Open your ~/.config/openbox/rc.xml in your favorite editor. Also open a terminal window. In the terminal window start xev to find out what keys you are pressing:

$ xev

xev is the X Event monitor, which shows you all events that X receives and dispatches. You should be aware that it will output any event that occurs, so the trick is to know what you’re looking for. We’re looking for Key events, so it’s probably easiest grepping the key events from the result:

$ xev | egrep '^KeyPress' -A5

Now press the keys you wish to know the keycode of. Openbox’s rc.xml will need the name part of the keysym. For example pressing your Escape button would yield something like:

KeyPress event, serial 39, synthetic NO, window 0x1800001,
root 0x15a, subw 0x0, time 3512708, (-289,-351), root:(1862,445),
state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,
XLookupString gives 1 bytes: (1b)
XmbLookupString gives 1 bytes: (1b)
XFilterEvent returns: False

The part you’re looking for is:

state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,

Now, do the same for the +, – and Mute buttons on your keyboard. Press them and find out what code X dispatches. You’ll find XF86AudioRaiseVolume, XF86AudioLowerVolume and XF86AudioMute are the codes you’re looking for.

Now, we need a program to influence mixer control. The easiest is to use amixer, which is the alsa command line tool to control the entire mixer. To change the volume, you can pass the mixer control name and some commands to the amixer set command. Here’s my config:

<keybind key="XF86AudioRaiseVolume">
    <action name="Execute">
        <command>amixer -q set Master 2+ unmute</command>
    </action>
</keybind>
<keybind key="XF86AudioLowerVolume">
    <action name="Execute">
        <command>amixer -q set Master 2- unmute</command>
    </action>
</keybind>
<keybind key="XF86AudioMute">
    <action name="Execute">
        <command>amixer -q set Master toggle</command>
    </action>
</keybind>

Of course, this isn’t really rocket science, but since now you know how to find out what key code you are pressing, you can think of all kinds of exciting things to do with all those special buttons at the top of your keyboard. Go crazy 🙂

This entry was posted in Linux & BSD and tagged , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Subscribe without commenting