Changing key bindings in Linux

While most people are happy with their keyboard mapping the way it is there are some that would like it if some keys were in a slightly different place (this would apply to many Emacs users) or to be able to change the action of keys they may never use. To do this is actually easier than you may think and modern desktop environments actually have limited inbuilt functionality for this. But when I say limited I mean Limited mostly only applying to a few keys such as ALT CTRL and CAPS, but bellow I will show you not only this but with a bit of help from a few pieces of software it is possible to change the bindings on almost any key and even add your own shortcuts.

The inbuilt functionality in desktop environments is not hard to find, for KDE at least it is located in the system settings under input devices->keyboard->advanced. below is an image of what you can expect to find.

keyboard_layout

The blue selected check-boxes are indicating the options you will most likely be interested in. Since it is so easy to use and find once you know it is there I will not go further into detail about this but instead focus on what to do if you want to take your customisation a step further.

To do this you will need xbindkeys and xdotool while there are other options such as the  xautomation software instead of xdotool they are both used in a similar way and by looking at the man pages you should be able to work out what to do to swap one for the other.

Once you have the two bits of software installed you will want to find the .xbindkeysrc file that should be located in your home directory if it is not there create it. This file is where you will be placing all your key bindings you are going to create.

The bindings you create will be a pair of lines one containing a command to run and a second line with the key code and state, you can add an optional comment to help you remeber what the binding is for, note that anything after a “#”is a comment and will not effect the binding

# binding name

"command"

state + key code

After adding the command you would like to use you will then need to find the key code and state for the key/s to trigger that command luckily to get the state and key code xbindkeys has a command. (if you want to emulate another key press how to do that is listed below, for now just leave the line blank)

xbindkeys -k

This command will display a dialog box, you then press the keys that you want to use for the key binding, it will then display the state and key code for those keys in a format such as bellow.

m:0x8 + c:32

You then copy that code into the your .xbindkeysrc file in the appropriate place and you have created you binding for that key or combination of keys.

# binding name

"command"

m:0x8 + c:32

This will work for keyboard shortcuts for scripts but to get the command to send a keyboard event signal you need to use xdotool which gives you the ability to send keyboard events using commands from the command line. To do this is rather simple and all you need to do is add the xdotool comand with the key you want to send (more info can be found in the man pages man xdotool)

xdotool key selected-key

As and example

# binding name

"xdotool key crtl+r"

m:0x8 + c:32

Remember to put the command in quotes

Using these tools you can modify your keyboard mapping and shortcuts as much as you want. But if you find this is more than what you wanted you may also want to look into xmodmap which also has a graphical front end called xkeycaps.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s