Execute A Command Of Your Choice When You Press A Certain Key

If you work in the console, you can re-define the keyboard-mappings of any key, so that this key executes any command of your choice.

For this to work, you first need to find out the keycode of the desired key, e.g. F12. You do this by executing showkey, and then pressing F12. Showkey will now show you this output:


$ showkey
press any key (program terminates after 10s of last key press).
keycode 28 release
keycode 88 press
keycode 88 release

Keycode 28 (release) means that you just released your ENTER-key.
Keycode 88 (press and release) is shown because you just pressed and released F12.
The showkey command will end after you have waited 10 seconds without pressing any key.

Repeat this procedure as often as necessary to find out all the keycodes you need.

Now we can change the so-called Kernel String Table , by issuing the command loadkeys, and then entering your strings.


$ loadkeys
keycode 88 = F12
keycode 125 = F40
string F12 = "ls\n"
string F40 = "mikmod -rp /usr/local/data/mod/*.mod\n"

Now press CTRL-d to leave loadkeys. That's it.
Everytime you press F12 you will get the output of ls on your console, and everytime you press the left Windows-key (keycode 125) mikmod, my favorite MOD-player, will be started.
You can now execute your favorite (console-)applications with just one keypress.

You can of course automate this whole precess. Just put your "keycode"- and "string"-lines in a file, say ~/.keystrings, and put a similar line in your boot.local or a similar file:

loadkeys /home/user01/.keystrings

Now everytime you reboot your computer, your key-settings are automatically loaded.