Saturday, December 28, 2013

Become a key binding pro using xbindkeys

Hey all this time around Im going to show you how to set up key bindings using xbindkeys. I found the need for this after picking up a new mouse for my desktop, Logitech MX pro. I gotta say this is one beautiful mouse; darkfield laser, frictionless scrolling, too much. However it came with a few extra buttons not supported by default in Ubuntu, so of course I had to fix it. Now the following process should work on any Debian based system and any system using the xautomation package. So...... lets get started

STEP 1: Install all the packages
     Complete this using your favorite package manager. I will use aptitude in my examples.
 $ sudo apt-get install xbindkeys xautomation
STEP 2: Setup the default xbindings
    To make the necessary config file enter the command below.
$ xbindkeys --defaults > $HOME/.xbindkeysrc  
STEP 3: Find your keys
    This step works for not only a mouse, but also any key combo on your  keyboard.

    Lets run the event catcher. You can use either xev or xbindkey command. Use the -k option to catch a single event. Use the -mk option to collect multiple.
    $ xbindkey -k
If completed correctly the terminal will display something like:
"(Scheme function)"
    m:0x10 + c:248
    Mod2 + NoSymbol

This is a random button on my laptop which has no function..... till now :P
STEP 4.1: Lets edit  the config file
Use your favorite editor, I will show gedit in this example.
 $ sudo gedit .xbindkeysrc
I highly recommend removing all of the default emulations from the file. Otherwise some of your favorite shortcuts will be remapped. (About line 34 and above.) If not you can always fix it later.

Now just copy and paste your event from earlier into the file. Now, the line that reads "(Scheme function)" is the command to be run. In this example I would like it to open my calculator, being its location on the num pad.
"gnome-calculator"
    m:0x10 + c:248
    Mod2 + NoSymbol
This works a little differently when working when emulating keyboard shortcuts. Keep reading.

STEP 4.2: If you want to emulate a keyboard shortcut, i.e. use one button to "press" multiple, we need to use the xautomation package from earlier. In my example I use the thumb button on my mouse to open the unity dash and to open the window spread. The shortcuts are Super and Super+w respectively. To emulate keyboard commands we use the command "xte". Here is my command for the dash:
"xte 'key Super_L'"
b:13 + release
Use the "key" descriptor when there are no subsequent button presses. This command claims the the left super key. You could also use "Super_R" which is the right key. The "b:13" refers to the physical button of the mouse. Finally the "+ release" is very important. This assures that your finger has released the button before executing the command.

The next example is to open the window spread. This is very similar to the previous example. The main difference here is the use of the "key*" command. These descriptors assure that the middle button is pressed before the Super key is depressed.
"xte 'keydown Super_L' 'key w' 'keyup Super_L'"
b:10 + release
Just for reference here is what my file looks like.
 # For the benefit of emacs users: -*- shell-script -*-
###########################
# xbindkeys configuration #
###########################
#
# Version: 1.8.5
#
# If you edit this file, do not forget to uncomment any lines
# that you change.
# The pound(#) symbol may be used anywhere for comments.
#
# To specify a key, you can use 'xbindkeys --key' or
# 'xbindkeys --multikey' and put one of the two lines in this file.
#
# The format of a command line is:
#    "command to start"
#       associated key
#
#
# A list of keys is in /usr/include/X11/keysym.h and in
# /usr/include/X11/keysymdef.h
# The XK_ is not needed.
#
# List of modifier:
#   Release, Control, Shift, Mod1 (Alt), Mod2 (NumLock),
#   Mod3 (CapsLock), Mod4, Mod5 (Scroll).
#
# The release modifier is not a standard X modifier, but you can
# use it if you want to catch release events instead of press events

# By defaults, xbindkeys does not pay attention with the modifiers
# NumLock, CapsLock and ScrollLock.
# Uncomment the lines above if you want to pay attention to them.
#################################################################
#Thumb Button
"xte 'keydown Super_L' 'key w' 'keyup Super_L'"
b:10 + release

#Zoom Button
"xte 'key Super_L'"
b:13 + release

#Calculator
"gnome-calculator"
    m:0x10 + c:248
    Mod2 + NoSymbol

##################################
# End of xbindkeys configuration #
##################################

I hoped I was able to help! Have fun modding!

**The customary for those who care
   Ubuntu 13.10 saucy salamander, xbindkey