Tuesday, October 25, 2011

Opening new browser tab when Caps Lock key is pressed

After using a Chromebook for a while, I realised how useful mapping Caps Lock key to opening a new browser tab can be. Of course, it’s possible to set up key bindings to achieve this in Linux.

First, I set up .Xmodmap so that pressing Caps Lock is interpreted as the same as pressing Calculator key on my multimedia keyboard. I chose Calculator key because it doesn’t currently do anything, and I don’t use it at all. I added the following lines to my ~/.Xmodmap file.

remove Lock = Caps_Lock
keysym Caps_Lock = XF86Calculator

Now, pressing Caps Lock would be the same as pressing Calculator key.

We need to make pressing Calculator key send Ctrl+T keystrokes instead. This can be done in KDE by defining a new global shortcut. In KDE 4.7, this is done by navigating to System Settings > Shortcuts and Gestures > Custom Shortcuts. Define a new Command/URL global shortcut. Use Caps Lock as the trigger shortcut (it would show as Calculator in the UI). Specify

/usr/bin/xte "keydown Control_L" "key t" "keyup Control_L"

as the command to run. (You’d have to install xte if it isn’t already installed on your machine.) That’s it; now pressing Caps Lock anywhere within KDE would send Ctrl+T keystrokes instead.

A few tips:

You can use xmodmap -pk command to see the list of all available keys.

Be sure to select a key that’s actually present on your keyboard; my laptop does not have a calculator key, so I am using the battery key instead. Any key that's present in the keyboard but not currently in use would do.

After you have modified your ~/.Xmodmap, you’ll have to log out and log back in for the mappings to apply. Alternatively, you can apply the configuration to your current session from the command line, e.g. by running xmodmap -e "remove Lock = Caps_Lock".