I never really understood how a terminal emulator (be it e.g. XTerm or the Linux virtual console) works. What transformations happen to the input (key up/down events) to produce the output (usually the standard input of the application that is running in the terminal, a signal being sent and so on)? What is actually the input for the terminal and what is already handled by the input drivers (in the kernel, X server, ...). How and where do the terminal capabilities databases like terminfo or termcap enter this process? How are "special" keys like arrows processed? And conversely: how does it work the other way round: application standard output/error → medium (screen, paper)?