multimedia keys

In Linux, multimedia keys have always been a bit difficult to configure. Sure,
there are third party programs which help, but there’s an easier, more
fundamental way, using xmodmap. What xmodmap does it remap the
keys and give them corresponding X identifiers. For example, this is
how my ~/.xmodmap file looks like:

! This works with Trust Silverline Direct Access keyboard
! Use ! for comments

keycode 222 = XF86PowerOff
keycode 223 = XF86Sleep
keycode 236 = XF86Mail
keycode 229 = XF86Search
keycode 230 = XF86Favorites
keycode 178 = XF86WWW

keycode 162 = XF86AudioPlay
keycode 164 = XF86AudioStop
keycode 160 = XF86AudioMute
keycode 144 = XF86AudioPrev
keycode 153 = XF86AudioNext
keycode 176 = XF86AudioRaiseVolume
keycode 174 = XF86AudioLowerVolume

As you can see, the various keycodes have been assigned to the X key
symbols; this enables whichever window manager you use to understand
the keysym. Now you can associate the keysyms with commands, etc. In
Arch a list of keysyms is at /usr/share/X11/XKeysymDB. To apply
these settings simply run xmodmap ~/.xmodmap

multimedia keys control whichever application is active

There are many programs which do not natively support the multimedia
keys, for example the Totem media player. Also you can bind only one
command to be the keybinding for the keysym. So I have made a small
script in bash which detects the active window, and depending on what
window it is, gives the appropriate commands. For example, the script
for the play key is:

# Program to start playing in whichever application
# is the focussed one.

# Abhishek Dasgupta 
# get focussed window id
focus=`xdpyinfo | grep focus | cut -c 16-24`
# grab WM_CLASS
info=`xprop -id $focus | grep WM_CLASS | cut -c 20-`

if [[ "$info" =~ .*Audacious.* ]]
  audacious --play
elif [[ "$info" =~ .*Totem.* ]]
  totem --play
  mpc play

You can grab the full set of scripts:

Currently, as you see the scripts support audacious, totem and mpc.