Binding Media Keys in i3 on Arch Linux to Control MPD

My Log­itech key­board has sev­eral but­tons at the top that I wanted to use to con­trol mpd (man|arch). They were Prev, Play/Pause, Next, and Mute/Unmute.

I knew that my win­dow man­ager i3 (man|arch) was able to bind key com­bi­na­tions to per­form func­tions such as exe­cut­ing a pro­gram, switch­ing the vis­i­ble work­space, or mov­ing a win­dow. The for­mat of the .i3/config file to launch a pro­gram on a cer­tain key com­bi­na­tion is:

bindsym <key com­bi­na­tion> exec <com­mand to execute>

With that known, we need to know how the sys­tem refers to those media keys. This is where xev (man|arch) is use­ful. Xev is a pro­gram that will print out data about mouse move­ments, key press events, and key release events. When the key is unbound you’ll get a Key­Press event like the below:

Key­Press event, ser­ial 32, syn­thetic NO, win­dow 0x4800001,
    root 0x28e, subw 0x0, time 654012911, (-169,-224), root:(1755,327),
    state 0x10, key­code 20 (keysym 0x2d, minus), same_screen YES,
    XLookup­String gives 1 bytes: (2d) “-“
    XmbLookup­String gives 1 bytes: (2d) “-“
    XFil­terEvent returns: False

Notice the sec­tion on the 2nd line, “(keysym 0x2d, minus)”, in this exam­ple, I had pressed the “-” key and found that the sys­tem refers to it as “minus”. I did this for each of the media keys and found their names to be:
Prev: XF86AudioPrev
Next: XF86AudioNext
Play/Pause: XF86AudioPlay
Mute/Unmute: XF86AudioMute

We can now fill in the key com­bi­na­tion sec­tion of the .i3/config:

bindsym XF86AudioPrev exec <com­mand to exe­cute>
bindsym XF86AudioNext exec <com­mand to exe­cute>
bindsym XF86AudioPlay exec <com­mand to exe­cute>
bindsym XF86AudioMute exec <com­mand to execute>

By read­ing the man page of mpc(man|arch), we know that it can per­form the fol­low­ing func­tions given the cor­re­spond­ing com­mand:
Prev: mpc prev
Next: mpc next
Play: mpc play
Pause: mpc pause

This makes two of the key bind­ings straight for­ward, but leaves us need­ing a tog­gle for Play/Pause, as we have one but­ton that must send one of two com­mands depend­ing on the cur­rent state of mpd. Here’s a bash script that will per­form just such a tog­gle:

With the mpc_toggle script copied to /usr/local/bin our .i3/config now looks like:

bindsym XF86AudioPrev exec mpc prev
bindsym XF86AudioNext exec mpc next
bindsym XF86AudioPlay exec mpc_toggle
bindsym XF86AudioMute exec <com­mand to execute>

Next up, Mute/Unmute. Because I’m using the ALSA sound dri­vers I can use the tool amixer (man|arch) to con­trol the Mas­ter out­put. Again, here’s a bash script that allows for a tog­gle by read­ing the state of Mas­ter before set­ting it:

And that’s it once we drop the script in /usr/bin/local and update the i3 con­fig­u­ra­tion file. We can now play/pause, mute/unmute, go back a song, or skip to the next from the keyboard.

Final .i3/config snippet:

bindsym XF86AudioPrev exec mpc prev
bindsym XF86AudioNext exec mpc next
bindsym XF86AudioPlay exec mpc_toggle
bindsym XF86AudioMute exec my_mute

Rebuilt the Workstation

Today I decided to rebuild my per­sonal work­sta­tion again. I’d had Win­dows 7 installed since Novem­ber 2011 as I had started play­ing games like Skyrim, Dia­blo 3, and Guild Wars 2. I’ve real­ized that I haven’t really played a game in many months and have no spe­cific need for the Win­dows OS.

I’ve went with my old friend Arch and tried out a new win­dow man­ager (i3).

randr is now fully sup­ported within the nvidia binary dri­vers, so i3 had no trou­ble rec­og­niz­ing the mul­ti­ple mon­i­tors. Both .i3status.conf and .i3/config were straight-forward to edit to my liking.

So far so good!