Assigning keyboard shortcuts to applications

8th of October 2008

I was surprised how annoying this was to do, all I wanted was to have some keyboard shortcut that focused a particular application, or started it if it wasnt already running. Ctrl+F1 would activate firefox, Ctrl+F2 would bring up emacs, etc etc.

I found a good little program called wmctrl that allows you to activate and query desktop / window information, The following is what I did in ubuntu, I dont imagine other distros would be much different.

Install wmctrl

sudo apt-get install wmctrl

Then I wrote a little helper script, this does a match against the title bar of each window and activates the first one that matches. If it doesnt match anything, attempts to run the command :

#!/bin/bash
# given a application name, focuses the first window
# with its name in the title, or start it

NAME=$(echo $1 | tr "[:upper:]" "[:lower:]")
ID=$(wmctrl -l | awk -v win="$NAME" 'tolower($0) ~ win {print $1; exit}')

if [ -z $ID ]; then
    $NAME
else
    wmctrl -i -a "$ID"
fi

Store this anywhere you want, I called it focus, you can then run /home/name/focus Firefox to activate/focus firefox or emacs or any other application that has its name as the title.

As for assigning the shortcuts, its pretty simple, I just ran gconf-editor browsed to apps->metacity then set the following values

keybinding_commands global_keybindings
command_1
/home/dale/Scripts/focus Firefox   
run_command_1
<Control>F1
 command_2
/home/dale/Scripts/focus emacs
 run_command_2
<Control>F2
 command_3
/home/dale/Scripts/focus tilda
run_command_3
<Control>F3

Obviously this only works if the application has its own name in the title bar, luckily everything I wanted to assign shortcuts to did, but if you have a better method / workaround, just post it in the comments.

Comments


dh

Oct 18, 08
welcome to 1998

kostix

Oct 19, 08
fvwm can do this and it can locate windows by the name of their class; you can check out whether this program knows about class names since they must be non-empty if I understand right.

Post a Comment


Name :
Url :
Comment :
html is enabled, you may post links / images and basic formatting, styling is permitted.
75 minus five is :
Simple check against spamming robots