Linux Keystroke HOWTO by Zenon Fortuna (zenon@netcom.com) Version: 1.0 May 7th '94 INDEX 0. What is "Keystroke-HOWTO" ? 1. Short description 2. Tools for keyboard driver modification 3. Modifying keytable file 3.0 Example of keytable file modification 3.1 Temporary modification of the keyboard setup 3.2 Permanent modification 4. Example of the key_macro script 5. Comments 6. Further ideas ? ---------------------------------------------------------------------------- 0. What is "Keystroke-HOWTO" ? This document is for users, who want to assign special action to some of keys of the keyboard. The suggested method is to use the loadkeys(1) or to modify the defkeymap.c file and relink the kernel. The method described below was tested on Linux 1.0 release, packaged in the Slackware 1.2.0.x distribution. 1. Short description The Linux virtual terminal and keyboard drivers assume default keyboard mapping as defined in the drivers/char/defkeymap.c file of the kernel source. The 12 PC keyboard function keys may get strings assigned to their action. After pressing any of those function keys, perhaps modified with the Alt or Ctrl keys, the current virtual terminal adds the specific string to its input and output buffers, in effect emulating entry of this string as typed in from the keyboard. Setting an appropriate string for chosen function key, we can simplify execution of selected command, for example calling a Shell-script "/usr/local/bin/key_macro", which we can create and modify as desired. 2. Tools for keyboard driver modification We may use loadkeys(1), dumpkeys(1) and showkey(1): The loadkeys(1) utility helps to load new strings into the kernel keyboard buffers or prepares the new C-code to modify the kernel. The dumpkeys(1) should be used to get the current keyboard mapping table for inspection or modification. The showkey(1) may assist us to obtain the keycode of the selected function key. If your Linux system does not have these utilities, you may get them via anonymouse ftp as kbd-0.87.tar.gz package from sunsite.unc.edu:/pub/Linux/system/Keyboards, or tsx-11.mit.edu:/pub/linux/sources/system 3. Modifying keytable file Linux kernel includes compiled defkeymap.c code, which is generated with the loadkeys(1) utility from a defkeymap.map file. Both files are included in the src/linux/drivers/char directory. We need to modify the defkeymap.map file, so let's make a local copy of it either by # cp defkeymap.map my_keytable.map or # dumpkeys > my_keytable.map There is also a large collection of different keytable files in the /usr/lib/kbd/keytables directory, from which "defkeym.map" is identical to the src/linux/drivers/char/defkeymap.map file. The method which uses the dumpkeys(1) utility is recommended, because it may happen, that our kernel was already modified or generated for us with different defkeymap.map file than the one we can find. Lets read the contents of our my_keytable.map file: there are more than 300 lines of code, and we can find 3 groups of declarations. The first group begins with words "keycode" or "alt keycode" or "control keycode" or "shift keycode". The second group begins with the word "string". The third group begins with the word "compose". More about the keytables(5) syntax can be read with % man keytables 3.0 Example of keytable file modification As an example of assigning a macro-string to a function key stroke, let's make the "Ctrl-F1" to call our "/usr/local/bin/key_macro" Shell-script. First of all we should find out what is the keycode for the F1 function key. We may use the showkey(1) utility to find the keycode with pressing F1. Instead we can search for the "F1" string in the "my_keytable.map" file to find the following line: keycode 59 = F1 F11 Console_13 This means, that the keycode for the F1 function key is 59. This line defines also, that after pressing the F1 key the keyboard driver would send out the string denoted by the string-code "F1". To see the contents of this string, one can search for the "string F1" pattern, to find string F1 = "\033[[A" This means, that after pressing the F1 key, the keyboard driver sends the "Esc [ [ A" (without blank spaces). We shouldn't change this string, because some applications depend on this string as default action of the F1 function key. However, below the "keycode 59 =..." line we may read a line defining the Ctrl-F1 action: control keycode 59 = F1 This essentially means, that pressing "Ctrl-F1" we send the same string out (denoted by the string-code F1) as pressing the plain "F1" key. To change it we should find an unused string-code name. A good candidate could be the F26 string-code, which in the default defkeymap.map file denotes an empty string in the following line: F26 = "" Instead, let's change it in "my_keytable.map" to F26 = "/usr/local/bin/key_macro\n" Then let's change the shown above line defining Ctrl-F1 action, to control keycode 59 = F26 In the summary, we made two changes to the original "my_keytable.map" file: we declared the new value to the F26 string and we have defined the Ctrl-F1 calling the F26 string. 3.1 Temporary modification of the keyboard setup Having properly modified "my_keytable.map" we can copy the changes to the kernel keyboard driver, using the loadkeys(1) utility: % loadkeys my_keytable.map The permission to modify the kernel keyboard driver is granted to everybody who has the read access to the "/dev/console" device. To verify that the intended changes were installed, we can use the dumpkeys(1) utility to check the F26 value, for example % dumpkeys | grep F26 We may see: string F26 = "/usr/local/bin/key_macro\012" which is OK, because "\012", or LF, is equivalent to "\n". Now, pressing "Ctrl-F1" should call the "/usr/local/bin/key_macro" Shell-script, as intended. 3.2 Permanent modification The changes to the kernel keyboard driver imposed by the loadkeys(1) last until the next reboot (or the next call to loadkeys). We can modify the /etc/rc.d/rc.local to call the loadkeys with our my_keytable.map file as an argument. Instead, we can modify the src/linux/drivers/char/defkeymap.c and re-link the kernel with new defaults. We should not modify the defkeymap.c manually, but rather generate it with the loadkeys(1) utility: # mv defkeymap.c defkeymap.c.ORIG # loadkeys --mktable my_keytable.map > defkeymap.c Then we should generate the new kernel, essentially changing directory to the root of the linux kernel source, and using the make(1). Finally, we should use the lilo(1) to boot with our new kernel. 4. Example of the key_macro script A particularly useful script for simple-key-stroke operation may be a Shell-script preparing, or printing, a screen dump. The code below should be regarded as an example of possible applications: #!/bin/sh # # This is an example of useful key_macro script # VT_NUMBER=`tty|cut -c9-` FILE=/tmp/vt$VT_NUMBER.dump setterm -dump $VT_NUMBER -file $FILE echo SCREEN DUMP saved in $FILE # # Uncomment the line below if you want to print the resulted dump-file # lpr $FILE The *problem* with the above "setterm -dump ..." call is, that it uses the ioctl(0,TIOCLINUX) system call, which is reserved for the Superuser only. This means, that the intended screen-dump would work only for "root". Oh, well. 5. Comments There is a limit to the sum the lengths of all strings which are to be copied to the keyboard driver: FUNC_BUFSIZE, declared in the "keyboard.h" is set to 512 bytes. In case of attempt to assign too long strings, the loadkeys(1) will fail with the message: "func-buf overflow". The defkeymap.map and generated getkeymap.c use about 100 bytes only. 6. Further ideas ? In case you find anything worth adding to this document, please send your comments to zenon@netcom.com -- thanks (zf)