Extra Keyboard Keys in Console
From ArchWiki
When we are in console, we can use our hotkeys to print a certain character. Moreover we can also print a sequence of characters and some escape sequences. Thus, if we print the sequence of characters constituting a command and afterwards an escape character for a new line, that command will be executed!
In order to do this, we could modify our console keymap. However, I suggest not to do this, since that is a delicate file and since it will be rewritten anytime we update the package it belongs to. It is better to integrate the existing keymap with a personal keymap. The utility 'loadkeys' can do this.
First of all, we need to write down this file. You can put it as you prefer, but I prefer to mimic partially the hierarchy of the default keymaps into /usr/local. So:
# mkdir -p /usr/local/share/kbd/keymaps # vim /usr/local/share/kbd/keymaps/personal.map
As a side note, it is worth noting that such a personal keymap is useful also to redefine the behavior of keys already treated by the default keymap: in fact, when we load it with 'loadkeys' the directives in the default keymap will be replaced when they conflict with the new directives and conserved otherwise.
Anyway, we need two kinds of directives in our personal keymaps. First of all, the keycode directives, with the format we have seen above in the default keymaps. These directives associate a keycode with a keysym. Keysyms represent keyboard actions. The actions available include outputting character codes or character sequences, switching consoles or keymaps, booting the machine etc. (The complete list can be obtained with
$ dumpkeys -l
Anyway, most of them are intuitive. If we want our key to output an 'e', the directive will be:
keycode 112 = e
If we want our hotkey to output the symbol of the euro currency, the directive will be:
keycode 112 = euro
Some keysym are not immediately connected to a keyboard actions. In particular the keysyms constituted by a capital F and one to three digits (F1-F246) constituting a number greater than 30 are always free. This is useful for us if we want our hotkey to output a sequence of characters and other actions. In fact we can first of all bind our character to such a keysym:
keycode 112 = F70
Then we use another kind of directive, which binds the keysym to an action. E.g., if we want our hotkey to output a literal "Hello":
string F70 = "Hello"
This can seem not very useful. But we can use it to print and execute commands. In order to execute the printed command, I need to use an escape sequence corresponding to a new line. E.g., I can want an hotkey to hibernate my laptop also when I am a user (through sudo). In this case I write the following directive in my personal keymap.
string F70 = "sudo /usr/sbin/hibernate\n"
In order to take profit of my personal keymap, I have to load it with 'loadkeys':
$ loadkeys /usr/local/share/kbd/keymaps/personal.map
Also in this case, this is valid only for the current session. In order to accomplish the same result each time I boot my machine, I have to insert another line in /etc/rc.local. This line should come after the occurrences of 'setkeycodes' discussed in section 2, since the personal keymap resorts to the keycodes assigned through 'setkeycodes':
loadkeys -q /usr/local/share/kbd/keymaps/personal.map&
The '-q' option simply avoids that a confirmation message is printed to stdin during the boot process.