Custom keyboard commands for the console

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 keymap file.

cp ~

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

cd ~

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

keycode 62

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 sudo), type:


Exit superuser, press F4, and see what happens. :D 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. ;)

5 thoughts on “Custom keyboard commands for the console

  1. quigybo

    In X I have been using xbindkeys for a while, pretty happy with it. Easy config in your .xbindkeysrc. You can get the keycodes in X using xev.

  2. Kaleb Elwert

    I sent you a message a while ago on the forums about this. It’s sweet that you figured it out. I’m tempted now to try a command line system again…

    Now just what to do if the key isn’t showing up on showkey

    1. K.Mandla Post author

      Oh good, I’m glad you saw this. I went looking for your PM after I figured this out, but I had deleted it. Cheers, and thanks for the note.

      By the way, sometimes proprietary keys have to be enabled in the kernel, so if (for example) e-mail and search keys on a Thinkpad don’t work, it might be because the kernel wasn’t configured to intercept them. …

  3. Pingback: Links 8/1/2010: New Palm Model, Beta Public of Boxee | Boycott Novell

  4. Pingback: Less than successful: mc as a daemon « Motho ke motho ka botho

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s