Originally, "tty" had two definitions: the hardware (now the emulator) and the driver (interfaced through /dev/pty* or /dev/tty*).
The hardware/emulator was/is responsible for:
- Taking a stream of data and presenting it; this included interpreting control sequences like "move cursor left", "blinking cursor", "clear-screen" although these control sequences were often different among manufacturers.
- Sending keycodes for keys typed by the user; most of these were standard ASCII characters, but some terminals sent propriety keycodes for even standard keys.
The 'tty' driver was responsible for:
- Managing the buffering, in raw or canonical mode; for example, buffering a line of characters until Enter is pressed.
- Managing the control flow; e.g. being able to stop/continue with Cntl-s/Cntl-q.
- Translating propriety keycodes to standard ASCII, where applicable.
- Intercepting certain control characters (like Cntl-c and Backspace) and processing them appropriately (send SIGINT on a Cntl-c or signal an EOF on Cntl-d.
- Canonical display of characters, for example, if
echo
is turned off, then do not send feedback (character typed) back to the terminal.
The terminfo and termcap databases managed what terminal control characters should be sent for an operation (like 'clear-screen'). These control sequences were/are not interpreted by the driver, but by the hardware/emulator.
The Linux kernel generates a code each time a key is pressed on a keyboard. That code is compared to a table of keycodes
defining a figure that is then displayed.
This process is complicated by Xorg
, which starts its own table of keycodes
. Each keycode
can belong to a keysym
. A keysym
is like a function, started by typing a key. Xmodmap
allows you to edit these keycode-keysym
relations.
To get the current keymap table using Xmodmap
use:
xmodmap -pke
This will print out the full table in the following format:
keycode <keycode#> = <boundkey> <boundkey>
Before moving anything around be sure to backup the original keycode
layout using xmodmap -pke >> $HOME/Xmodmap.orig
This will place the file Xmodmap.orig
in your users home directory.
Tip: There are also some predefined keycodes (e.g. XF86AudioMute
, XF86Mail
). Those keycodes can be found in: /usr/include/X11/XF86keysym.h
You can also also edit the keys: Shift
, Ctrl
, Alt
and Super
(there always exists a left and a right one (Alt_R=AltGr
)).
Here's a quick example of how your configuration would look if you wanted to swap CTRL
and Super
(Windows Key):
keycode 255 =
!add Shift = Shift_L Shift_R
!add Lock = Caps_Lock
add Control = Super_L Super_R
!add Mod1 = Alt_L Alt_R
!add Mod2 = Mode_switch
!add Mod3 =
add Mod4 = Control_L Control_R
!add Mod5 =
(the !
is used to comment / ignore the line. in this example only Super
and Control
keys get adjusted)
This configuration would be saved in $HOME/.Xmodmap
and loaded with
xmodmap ~/.Xmodmap
You could also start this with xwindows by adding it to your ~/.xinitrc
And if things get hairy you can always revert back to Xmodmap.org
.
Any bindings for applications that rely on these keys would also be moved. So make sure that everything remains bound so you don't lose any functionality. It's a tug-of-war match.
Best Answer
The kernel's terminal driver (
termios
) interprets the special keys that can be typed to send a signal to a process, send end of file, erase characters, etc. This is basic Unix kernel functionality and very similar on most Unix and Linux implementations.The
stty
command displays or sets thetermios
special characters, as well as other parameters for the terminal line driver.Invoke
stty -a
to see the current values of the special characters and other "terminal line settings". In the following examples, you can see thatintr
is Ctrl+C,eof
is Ctrl+D,susp
is Ctrl+Z. (I've deleted other output to show only the special character settings):stty -a
special chars on GNU/Linux:stty -a
special characters on FreeBSD:To change the value of a special character, for example, to change the interrupt character from Ctrl+C to Ctrl+E invoke
stty
like this (^E
is literally two characters, the circumflex (^
) followed by the letterE
):For more information see the man pages for
stty
andtermios
. On GNU/Linux you can also look at thetty_ioctl
man page.Notes:
The
intr
key (Ctrl+C by default), doesn't actually kill the process, but causes the kernel to send an interrupt signal (SIGINT
) to all processes within the process group. The processes may arrange to catch or ignore the signal, but most processes will terminate, which is the default behavior.The reason that Ctrl+d logs you out is because the terminal line driver sends EOF (end of file) on the standard input of the shell. The shell exits when it receives end of file on it's standard input.