I previously wrote about binding the left mouse button to the Caps Lock key. My goal was to use it specifically for Minecraft, however my implementation was broad enough to cover the entire OS. I've since written a wrapper script for the Minecraft launcher to deal with this. Let's take a look:
MCRUNNINGFILE=~/.minecraft_running
First off we need a way to keep track of whether Minecraft has been launched or not. For the sake of simplicity we assume that if we started it, it's still running. In this case, we're using a file as a flag to tell if it's running, so that this script can be called more than once without re-launching Minecraft.
if [ -z "$@" ]; then
if [ -e $MCRUNNINGFILE ]; then
echo Minecraft appears to be running already.
else
touch $MCRUNNINGFILE
java -jar Minecraft.jar
rm $MCRUNNINGFILE
fi
Next we check to see if the command line parameters to the script are empty. If so, then we must want to launch Minecraft. That is followed by a check to see if our running flag exists. If it does, we display an error and exit, otherwise create the flag, start Minecraft, and remove the flag when it's done.
else
case "$@" in
--help)
echo $0 Usage:
echo " $0 Launch Minecraft and bind the primary mouse button to Caps Lock"
echo " $0 --help Display this help"
echo " $0 --down Hold the primary mouse button down if Minecraft is running"
echo " $0 --up Release the primary mouse button if Minecraft is running"
echo " Note that the --down and --up options are only intended to be called from"
echo " xbindkeys. Doing so while xbindkeys is running can cause the state of"
echo " Caps Lock and the primary mouse button to become out of sync."
;;
--down)
if [ -e $MCRUNNINGFILE ]; then
xte 'mousedown 1'
else
echo Minecraft does not appear to be running.
fi
;;
--up)
if [ -e $MCRUNNINGFILE ]; then
xte 'mouseup 1'
else
echo Minecraft does not appear to be running.
fi
;;
*)
echo Unknown parameter. Try --help
;;
esac
fi
If the check for an empty command line fails, we use a case
to start comparing it to the various valid options. In this case, since only one option is valid at a time, I've kept it simple. In a perfect world I would have implemented something closer to this.
Running down the list, the first one is --help, which displays the program usage. Next are --down and --up, Both of these check the flag to ensure Minecraft is running and then launch xte to take the appropriate action with the mouse button. Lastly is the fall back if no other option matches. This is also where you would end up if multiple options were specified, such as Minecraft.sh --up --help as the script isn't smart enough to handle more than a single parameter on the command line.
Now that the script calls xte, we need to tweak our ~/bindkeysrc
to call the script instead. Edit it to look something like this:
keystate_capslock = enable
"/bin/bash ~/Minecraft.sh --up"
m:0x12 + c:66
Mod2 + Caps_Lock
"/bin/bash ~/Minecraft.sh --down"
Caps_Lock
Don't forget to restart xbindkeys:
killall xbindkeys
xbindkeys
Putting the script all together looks something like this:
#!/bin/bash
### Minecraft.sh
### Minecraft wrapper script to launch the Minecraft Launcher and
### enable/disable the Caps Lock/Left Mouse Button binding
## When called with no parameters the Minecraft Launcher is started
## When called with --down the primary mouse button is pressed*
## When called with --up the primary mouse button is released*
## When the script ends, the primary mouse button is released in the event that it
## might still be pressed
## * The options --down and --up are only effective if Minecraft is running
MCRUNNINGFILE=~/.minecraft_running
if [ -z "$@" ]; then
if [ -e $MCRUNNINGFILE ]; then
echo Minecraft appears to be running already.
else
touch $MCRUNNINGFILE
java -jar Minecraft.jar
rm $MCRUNNINGFILE
fi
else
case "$@" in
--help)
echo $0 Usage:
echo " $0 Launch Minecraft and bind the primary mouse button to Caps Lock"
echo " $0 --help Display this help"
echo " $0 --down Hold the primary mouse button down if Minecraft is running"
echo " $0 --up Release the primary mouse button if Minecraft is running"
echo " Note that the --down and --up options are only intended to be called from"
echo " xbindkeys. Doing so while xbindkeys is running can cause the state of"
echo " Caps Lock and the primary mouse button to become out of sync."
;;
--down)
if [ -e $MCRUNNINGFILE ]; then
xte 'mousedown 1'
else
echo Minecraft does not appear to be running.
fi
;;
--up)
if [ -e $MCRUNNINGFILE ]; then
xte 'mouseup 1'
else
echo Minecraft does not appear to be running.
fi
;;
*)
echo Unknown parameter. Try --help
;;
esac
fi
All that's left is to test it! See if Caps Lock will click on anything without Minecraft running (it shouldn't). Fire up Minecraft (I suggest tweaking your Menu or Launcher entry to run Minecraft.sh) and give it a whirl in-game. I can't imagine mining without it.