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:

#!/bin/bash
# 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.* ]]
then
  audacious --play
elif [[ "$info" =~ .*Totem.* ]]
then
  totem --play
else
  mpc play
fi

You can grab the full set of scripts:
mediabuttons-scripts-20070508.tar.gz

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

Advertisement

2 thoughts on “multimedia keys

  1. This is handy. I have a volume wheel, and it seems to be set to the proper volume keys (whenever I turn it in Emacs I get XF86AudioRaiseVolume is undefined). Is there someway to always have the key go to alsamixer irrespective of which window is up?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s