Keyboard and Console
From Initq
Contents |
The process
- Key is pressed
- Scancode sent by keyboards processor to the keyboard controller on the motherboard.
- Kernel sends the scancode to the keyboard driver.
- Keyboard driver converts the scancode to keycode then sends it to the application.
- Application programs utilize keycodes, characters and ANSI escape and control sequences; control sequences.
Keyboard generalities
Each key on the keyboard is assigned a unique key number which is called a scancode. The microcontroller in the keyboard continually scanns the keyboard for pressed keys. When you press a key, two scancodes are sent, one for press 'make' and one for release 'break'. This helps to detect a modifier key such as ctrl or shift. Some keys may even send more than two scan codes. The PS/2 keyboards are bidirectional, they can receive and send.
You press a key and the keyboard sends scancodes to the kernel keyboard driver. Some keyboards can be programmed, but usually the scancodes corresponding to your keys are fixed. The kernel keyboard driver just transmits whatever it receives to the application program when it is in scancode mode, like when X is running. Otherwise, it passes the stream of scancodes into keycodes, corresponding to key press of key release events. These keycodes are transmitted to the application program when it is in keycode mode. These keycodes are looked up in the keymap and the character or string found there is transmitted to the application, or the action described there is performed.
The translation between scancodes and keycodes can be set using utility setkeycodes. The translation between keycodes and charcater/strings/actions which is called the keymap is set using the utility loadkeys and setmetamode.
Where it says 'transmitted to the application' means that it is transmitted to the terminal driver. The details of this processing are set by a program stty.
Console generalities
When you output something to the console, it undergoes the standard tty processing and then is fed to the console driver. The console driver emulates a VT100 and parses the input in order to recognize VT100 escape sequences (for cursor movements, clear screen, etc.). The characters that are not part of an escape sequence are first converted into Unicode, using one of four mapping tables, UTF-8, then looked up in the font table, then written to video memory, where they cause the display of character shapes found in the video card's character ROM. One can load one's own font into character ROM using setfont.
Resetting your terminal
You can use Ctrl-L, clear or reset to clear the screen. You can also do
#!/bin/sh echo -e \\033c
Why is it that the display sometimes gets confused and gives you a 24-line or 1-line screen, instead of the usual 25 lines? Well, the main culprit is the use of TERM=vt100 (or some other entry with 24 lines) instead of TERM=linux when logged in remotely. If this happens on /dev/tty2 then typing
% cat > /dev/tty2 ^[c ^D
- stty sane
- Clean your broken terminal. When some console full-screen program (minicom, vi, some installers) breaks down your terminal, try this command to revert all options to "sane" settings (sane is a built-in combo of a lot of stty options)
- stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
- Read a keypress without echoing it. This shell snippet reads a single keypress from stdin and stores it in the $KEY variable.
You do NOT have to press the enter key! The key is NOT echoed to stdout! This is useful for implementing simple text menus in scripts and similar things.
- save_state=$(stty -g);echo -n "Password: ";stty -echo;read password;stty "$save_state";echo "";echo "You inserted $password as password"
- Hiding password while reading it from keyboard. Allow to read password in a script without showing the password inserted by the user
- stty -F "/dev/ttyUSB0" 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke time 5 min 1 line 0
- Configure a serial line device so you can evaluate it with a shell script. I had a hard time in finding the correct settings to get reasonable output from a coin selector which sends its data over a serial line. In the end, minicom came to the rescue and pointed me on the right track. So, if you need to do something similar, these settings may help you. Replace ttyUSB0 with your device file, 9600 with your baud rate, 5 with your read timeout (10ths of a second), and 1 with the minimum numbers of characters you want to read. You can then open the device file like you are used to do, example:
DATA="`xxd -ps -l 5 \"$DEV\"`"
- stty -echo; ssh -t HOSTNAME "sudo some_command"; stty echo
- Execute a sudo command remotely, without displaying the password. The ssh command alone will execute the sudo command remotely, but the password will be visible in the terminal as you type it. The two stty commands disable the terminal from echoing the password back to you, which makes the remote sudo act as it does locally.