Have you gone mad? Supporting Microsoft Products?!

Let me start by saying I'm not exactly a "fan" of Microsoft. In fact, for the most part I avoid their products whenever possible. There is one notable exception, and that is hardware.
Microsoft has produced some fairly nice and useful hardware. The Microsoft Natural Pro keyboard that I have is one of these nice pieces of hardware. It has some extra buttons at the top that provide one with instant access to, if nothing else, extra buttons. ;-)
Seeing as they are labeled, I've decided to put a few of them to use. My favorite button is the mute button. Perfect for those times where you're in the middle of a CounterStrike session and the phone rings.
Seeing as I use Linux.. I had to figure out how to make these buttons work on my own. So, here is the method I used.

Step one.. xmodmap.

Ok, the first step is mapping the raw keycodes to keystrokes that X applications understand. This is what xmodmap does.

For my setup, keycode 160 is generated when the Mute button is pressed. I found this out by using the program 'xev', or "X Event Viewer". It tells you in the terminal what events have been delivered to it. So I pressed the button while it had the focus, and I got this:

  KeyPress event, serial 27, synthetic NO, window 0x4000001,
  root 0x54, subw 0x0, time 3139698373, (-1270,697), root:(37,717),
  state 0x0, keycode 178 (keysym 0x1008ff18, XF86HomePage), same_screen YES,
  XLookupString gives 0 characters:  ""
  

Now.. actually.. this is for the "Mail" button, so it says 178 instead of 160. It appears differently when you have xmodmap to deal with it properly.
So, now that this is setup, I add this entry to ~/.Xmodmap:

  ! Mute
  keycode 160=F22
  

This should automatically load whenever X is started. You can also load it manually by typing:

  xmodmap ~/.Xmodmap
  

So now whenever you press the Mute key, it will register to X applications as "F22".

Ok, great, NOW WHAT?

Ok, well now is where things differ between window managers. I'm a weenie who uses(hey don't shoot me) KDE2. You might not think it very geekish to run such a windows-like environment, but I don't really care. KDE2 works, and lets me focus on what I really need to do. I'm not going to go any further on that.
Anyways, Here is how I did it under KDE2. If you have other window managers/environments that you have done this in too, let me know.

First of all, I created some scripts to call aumix. This might be the hard way, but here are my scripts:

  muteon:
  #!/bin/sh
  aumix -q | grep ^vol > .aumixmuted
  aumix -v0
  rxvt -geometry 40x1 -title "Mute On" -e sleep 1 &

  muteoff:
  #!/bin/sh
  if [ -e .aumixmuted ] ; then
    aumix -v`sed -e 's/vol //' .aumixmuted | sed -e 's/, .*$//'`
    rm .aumixmuted
  else
    aumix -v75
  fi
  rxvt -geometry 40x1 -title "Mute Off" -e sleep 1 &

  aumute:
  #!/bin/sh
  MUTEOFF=~/muteoff
  MUTEON=~/muteon
  volume=`aumix -q | grep vol | sed -e 's/vol //' | sed -e 's/, .*$//'`
  if [ $volume -eq "0" ] ; then
    $MUTEOFF
  else
    $MUTEON
  fi

  

I had to do it this way because I couldn't find a "toggle mute" command line utility.

So now I just need KDE to call aumute whenever it gets an F22. I do this by creating a menu shortcut(could be desktop, but I'm not a big fan of these).

Here's my volup and voldown scripts too. These get passed an argument, the number of percentage points to move up or down.

  volup:
  #!/bin/sh
  volume=`aumix -q | grep vol | sed -e 's/vol //' | sed -e 's/, .*$//'`
  if [ $volume -lt "100" ] ; then
    let newvolume=$volume+$1
    aumix -v$newvolume
  fi

  voldown:
  #!/bin/sh
  volume=`aumix -q | grep vol | sed -e 's/vol //' | sed -e 's/, .*$//'`
  if [ $volume -gt "0" ] ; then
    let newvolume=$volume-$1
    aumix -v$newvolume
  fi