Saturday, June 23, 2007

Keys and Linux Terminal Configuration

To really understand and work with key mapping, you should read the "Support for text" section in the "How Linux Works CTDP Guide". It explains how terminal types are set and the different layers of keymapping along with how to map keys for various applications. Linux keymapping is complex and this section will just give you a short explanation of the various utility programs available, and how to do some specific keymapping. If you must perform custom keymapping, the "How Linux Works CTDP Guide" is necessary.

Linux keymapping is performed on several levels and many programs such as the shell, less, and emacs have their own support for keymapping. Other programs such as terminal programs use the terminfo or termcap database for keymapping. The main system wide keymapping the user may deal with is keymap which maps the keycode to a set of ASC characters that each keystroke will produce. A file is created which contains these specific keymappings and it is loaded with the command "loadkeys". There is a man page describing the structure of this file. It can be accessed by typing "man keymaps". There are many symbols in this file that describe values for keys. You can see the value of many of them by typing "dumpkeys --long-info" or "dumpkeys -l". Once the keymap file is created, the easy way to permanently modify the key settings for the system is to modify the file /etc/sysconfig/keyboard to a new default value such as KEYTABLE="/etc/sysconfig/console/mykeymap".

Console key utilities and files

  • Kernel keyboard driver
    • kbd_mode - Report or set the keyboard mode to -s (scancode mode, RAW) -k (keycode mode, MEDIUMRAW) -a (ASC mode, XLATE) -u (UTF-8 mode, UNICODE). XLATE is default.
    • kbdrate - modify the keyboard repeat rate and delay time after keypress.
    • kbdconf - Configures the /etc/sysconfig/keyboard file which specifies the location of the keyboard map file.
  • For system key mapping make codes to keycodes
    • setkeycodes - used to set a translation between unusual scancodes and keycodes. This utility is not usually needed.
    • getkeycodes - Print the kernel scancode to keycode mapping table.
  • Mapping keycodes to keymaps
    • dumpkeys - dump keyboard driver translation tables in the format specified by keymaps. The output of this program will look similar to the keytable file in "/usr/lib/kbd/keytables/*.map" and the output can be saved to a file, and used to generate a new file to be used with the "loadkeys" command.
    • loadkeys - Load the keyboard translation table into the system from a specified file. Type "loadkeys -d" or "loadkeys defkeymap" to load the default keymap. Changes keys for all virtual consoles. The keyboard translation table set in the kernel (compiled in) is in the file "/usr/src/linux/drivers/char/defkeymap.map". To change the kernels keymap without having to load a new table after booting, you must modify the file, then re-compile the kernel. You would only need to do this to modify keymaps for remote boot systems that don't have access to all system programs.
    • setmetamode - define the keyboard meta key handling. Controls whether the keystroke , would send the keycode M-key or the key sequence ESC followed by key. You can have different setting in different virtual consoles.
  • Debugging tools
    • showkey - Examine the scancodes and keycodes sent by the keyboard. Options are -s (show scancodes) -k (show keycodes, default) -m (show keymap). This program outputs key values that the user presses on the keyboard. The program terminates when no keys are pressed for 10 seconds.
  • Termcap and terminfo
    • tic - Compiler for terminfo.
    • infocmp - Used to examine terminfo files.
    • /etc/termcap - The termcap database file.
    • /usr/share/terminfo - The directory structures containing the terminfo compiled database.
  • Other tools
    • consolechars - Load console screen font (was called setfont). The directory "/usr/lib/kbd/consolefonts/" is the default directory for fonts. "/usr/lib/kbd/consoletrans" is the default directory for ACM's and SFM's.
    • psfaddtable - Add a Unicode character to a console font.
    • psfgettable
    • pfsstriptable
  • For terminals and terminal key mapping
    • mesg - Use to turn others' write messaging ability to your terminal by on and off. "mesg on" to turn it on.
    • stty - Print or change the terminal line settings.
    • setterm - Set terminal attributes.
    • tput - Initialize a terminal or query terminfo database.
    • setleds - Set or output the keyboard LED settings for caps lock, num lock, and scroll lock.
    • setfont - Never use unless you know what you are doing. There is no man page for this command.
    • chvt - Change virtual terminal. Usage: chvt3.
    • deallocvt - De allocate unused virtual terminals
  • For X
    • xmodmap - Used to modify keymaps and pointer button mappings in X. Map X keycodes to keysyms. Usage "xmodmap mykeys.map".
    • xkeycaps - A GUI front end to xmodmap.
    • xev - Allows you to see key make and breaks generated on a window.
    • showfont - font dumper for X font server
    • xset - Set user preferences for the display.
    • /etc/X11/xinit/Xmodmap or $HOME/.Xmodmap - File(s) that can be uset to set keycode mapping in X
    • $HOME/.Xinitrc - A file run when X starts that is a script file. The program "xmodmap" can be invoked in this file to set keymappings for X.
    • $HOME/.Xresources - Another file where keymappings may be changed for X.
  • Bash
    • /etc/inputrc or $HOME/.inputrc - Where Bash keymapping is done.
  • Less
    • $HOME/.lessrc - Where keymapping is done for less.
  • Emacs
    • $HOME/.emacs - Where keymapping is done for emacs.
  • tsh
    • bindkey - A program supplied by tsh to allow for binding keymaps.
    • $HOME/.cshrc

Setting Keystrokes

The stty command - Used to set up keystrokes on terminals
The command will bind the Backspace key(^H) to the erase function.
Characters that are specified:

  • eof - End of file
  • eol - End of line
  • eol2 - Alternate character for ending a line
  • erase - Erase the last character typed
  • intr - The specified character will send an interrupt signal
  • kill - Erase the current line.
  • quit - Sends a quit signal.
  • start - Restart output after having stopped it.
  • stop - Stops the output.
  • susp - Sends a terminal stop signal
  • switch - Switch to a different shell layer
  • werase - erase the last word typed.
setterm -foreground white - background blue
Sets white foreground, and blue background
setterm -store
Stores the present terminal values as default

Keys and terminal configuration

Special command line keys:

  • CTRL U - Delete the line
  • CTRL C - Abort command
  • CTRL Z - Suspend command, Type fg to put it back into the foreground
  • - Auto complete command
  • F1 through F6 - Select a different virtual terminal
  • F7 - Toggle to the first X terminal, if one is running.
  • F1 - Toggle to the first text terminal from an X session.
  • CTRL D - End of file, exits the shell
  • CTRL W - Delete a word backwards
  • CTRL / - Quit, weaker than CTRL C but does a core dump.
  • CTRL S - Stop scrolling, may use "scroll lock" for this function.
  • CTRL Q - Resume scrolling

Emacs keymapping

To modify the keymap in emacs so pressing the end key will move the cursor to the end of a line rather than the end of file, add the following lines to your user's $HOME/.emacs file:

(define-key function-key-map "\e[4~" [end])
(global-set-key [end] 'end-of-line)

No comments: