Cueing a particular key to trigger an event in the console is easier than it sounds. Or at least, it’s easy after the Arch Linux wiki explains it to you, and you take the four or five minutes required to learn how it’s done. In my case I wanted to be able to trigger a particular program from within screen, along with a window title, but I don’t want to mess with screen’s keybindings. Ordinarily, that looks like this:

screen -t charm charm

From a bash prompt or from within screen itself, after pressing CTRL+a and a colon (:), that will trigger a new “window,” with charm inside it, already given the title. Rather than type that in, I’d like to hit the F4 key, so that particular string is fed to the machine.

The first trick is to create a custom keymap, and those are part of the kbd package. Some distros (like Slitaz base flavor, for example) require you to install that, but ordinarily that’s a core package and part of any Linux system. Keymaps are usually deep in the /usr directory, at

cd /usr/share/kbd/keymaps/i386/qwerty/

Remember that if you’re using a different architecture or a different keyboard, that location will vary just a little. I strongly suggest copying out the keymap you use and editing your copy; tampering with the defaults could have disastrous effects. I use the 106-key Japanese keyboard, so mine is the jp106.map.gz keymap file.

cp jp106.map.gz ~

Now un-gunzip it, so we can easily edit the contents, and rename it so it doesn’t get confused with other stuff.

cd ~

gunzip jp106.map.gz

mv jp106.map mykeys.map

Now take a short break, and change directions. What happens when you press a key at the console? A small message is reported to the system, and it responds as is appropriate. To find out what’s being sent to the system, we need the showkey utility.

showkey

Pressing a key now will spit out a code to the screen, and that’s the number we want for later. In this example, the F4 key results in

keycode 62

Wait 10 seconds, and showkey will end. Now let’s edit that map file.

vim mykeys.map

Add a line to intercept that key, and assign it to a consequence. Here, I use the F70 consequence, for no real reason other than the Arch wiki used it, and it’s not otherwise engaged. (Unless you know what you’re doing, you probably don’t want to pick a “claimed” F-number.)

keycode 62 F70

So now the system knows when I press F4, it should jump to the F70 instruction, and do it. So what is it? Add this on the next line.

string F70 "screen -t charm charm

"

That’s the command from earlier — followed by a backslash, and an “n”, which is a code for a carriage return. In other words, you’re telling the system, “When I press F4, send all these characters to the console in this order, followed by an Enter key.”

Save the file, and you’re done. … Almost. The next step is to load that keymap, because as it is now, all you’ve done is tinker with a pretty text file. On my system, only root can change the keymap, so after changing to the superuser (or by prefixing this with sudo ), type:

loadkeys mykeys.map

Exit superuser, press F4, and see what happens. 😀 Edit your startup configuration (/etc/rc.conf or even put that same command in /etc/rc.local), and your keyset will change on boot. If it’s all wrong and you hate it, switch back to the old keymap you started with, down in the /usr directory.

Again, check the Arch Linux wiki — by far the best wiki there is for Linux, of any flavor — if you want more details or a better explanation. Be aware that keys are handled slightly differently under X; I don’t use X and so I don’t have many tips for you there.