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
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.
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.
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
Wait 10 seconds, and showkey will end. Now let’s edit that map file.
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\n"
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
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.
But you can use this for just about any console command, although dense commands with loops and pipes might get tricky. So far I’ve used this to cue volume controls (with
amixer sset PCM 1+ and
amixer sset PCM 1-) and a few other little things I get tired of typing. Depending on your machine and your software, you might be able to use this to program special keyboard function keys or dedicated hardware keys. No guarantees, but leave a note if something in particular works for you.