How to make media keys work with i3

i3keyboardkeyboard shortcuts

I've moved from Gnome to i3 on Manjaro, and I'm almost done with configuring the window manager, and terminal colors and whatnot. After some time I just decided to listen to some music, and after a couple of minutes I realized that the volume keys and playback keys don't work.

I have a Razer Blackwidow Stealth 2014 keyboard, so those media keys are actually together with the Function keys. For example: Play/Pause is on F6, and it acts as a media key when I press the Fn key, like in Fn + F6.

Best Answer

The search for the answer

After some time messing around with the controls, I've found a post on the old i3 FAQ board: https://faq.i3wm.org/question/3747/enabling-multimedia-keys.1.html

It says to paste the following into i3's .config file (bellow is a lightly modified version, with some lines removed, which are not relevant to this particular question):

# Pulse Audio controls
bindsym XF86AudioRaiseVolume exec --no-startup-id pactl set-sink-volume 0 +5% #increase sound volume
bindsym XF86AudioLowerVolume exec --no-startup-id pactl set-sink-volume 0 -5% #decrease sound volume
bindsym XF86AudioMute exec --no-startup-id pactl set-sink-mute 0 toggle # mute sound

# Sreen brightness controls
bindsym XF86MonBrightnessUp exec xbacklight -inc 20 # increase screen brightness
bindsym XF86MonBrightnessDown exec xbacklight -dec 20 # decrease screen brightness

# Media player controls
bindsym XF86AudioPlay exec playerctl play-pause
bindsym XF86AudioPause exec playerctl play-pause
bindsym XF86AudioNext exec playerctl next
bindsym XF86AudioPrev exec playerctl previous

And it didn't work either, however the process of finding the answer is correct.

The real answer

To me, at least, the problem was that after copying those lines, the keys would not work. After some more research, I found out that the volume commands could be a little different, using amixer instead of PulseAudio's pactl.

At the end, those were left like this:

# Media volume controls
bindsym XF86AudioMute exec amixer sset 'Master' toggle
bindsym XF86AudioLowerVolume exec amixer sset 'Master' 5%-
bindsym XF86AudioRaiseVolume exec amixer sset 'Master' 5%+

and they started working.

The playback keys were a little more trickier. I deduced that the .config tells which command is executed to do the action. Then I proceeded to try playerctl play-pause on my terminal. Of course it didn't work, because playerctl was not installed. After installing it (using sudo pacman -S playerctl) those keyboard commands worked just fine too.