Phil Storrs PC Hardware book

The PC Keyboard operation

The IBM PC was equipped with a keyboard that had 84 keys, with the function keys on the left side of the keyboard. The keyboard was connected to the PC using a 5 pin DIN connector and this was plugged into a socket on the rear of the system board.

The IBM AT introduced a 101 key keyboard and this soon became the standard for most PC clones. This keyboard had the function keys along the top of the keyboard and a Numeric Keypad at the right hand end. There was also two extra function keys provided on this keyboard. The latest trend is to an Ergonomic Keyboard design like this one from Microsoft.

Keyboard Basics

A typical modern computer keyboard consists of processor chip connected to a matrix of keys arranged in an array of X and Y scan lines. The keyboard processor puts an active low, "one of" count on each output line in turn, and then it scans the input lines, one at a time, for a low. The input lines are pulled high by pull-up resistors, but will be pulled low if jointed to an active low output line by a key press.

The first PC keyboards used a 8048 processor chip and an 8 by 11 or 12 matrix. The matrix usually consisted of eight output lines and 11 or 12 input lines. Later PC keyboards used 8049 processors, and the 101 key keyboard needs at least a 10 by 11 matrix to access all the keys.

The keyboard processor uses a look-up table to find a code that represents the key that has been pressed. When the keyboard processor finds a "Key Press" during its scanning of the matrix, it sends a code to represent that key, to a keyboard interface circuit in the computer. In the past many computer systems have used ASCII codes to represent the keys in the matrix but the DOS computers use quite a complex procedure of Press and Release codes.

Keyboard mechanics

Three types of keyboard contacts (switches) have been used in PC keyboards
  1. Mechanical contact. These have a toggle action and usually provide good tactile and audible resposes. The better quality keyboards have a life expetancy of one or two million key presses per key.

  2. Coductive rubber pad. This type of keyboard has improved a lot over the years. The earliest examples had poor feel and were often quite unreliable. Today this type of keyboard is available in both soft-touch and click type and is very reliable.

  3. Capacitive contact. These high quality keyboards were popular in the early days of PC computers but are very expensive to produce. The original IBM PC, PC/XT and PC/AT computers used this type of keyboard. The key matrix is a series of very tiny capacitors joining the X and Y lines of the matrix. The key tops press down a capacitive plate close to two capacitive plates on the circuit board inside the keyboard, thus rising the capacitance between the X and Y lines of the matrix.

Keyboard connectors

The PC keyboard is connected to the computer via one of two types of connector.

Until recently, most PC computers used a 5 pin DIN connector on the computers system board and this supplied plus 5 volt and ground to the keyboard, and received data and clock signals from the keyboard, and supplied them to the keyboard interface circuit on the system board.

The pins on the 5 pin DIN connector are assigned thus:

The IBM PS/2 range of computers introduced a smaller connector, a Miniature 6 pin DIN, and this PS/2 interface is becoming the new standard. Many modern system boards have both a 5 pin DIN and a Miniature 6 pin DIN connector as keyboard interface sockets. Adaptors are available to adapt both types of connectors between the keyboard and the system board. Caution, this is the same connector as used for the PS/2 mouse interface and some system boards have a PS/2 mouse connector on the rear of the board.

The pins on the PS/2 keyboard connector are assigned thus:

The DOS computers keyboard

When we press a key on a PC keyboard a Press Code is generated by the action of pressing the key and a Release Code is generated when the key is released. The press and release codes are used to produce the "Typmatic" multiple key stroke feature.

The data from the keyboard is sent to a Keyboard Interface Circuit on the System board in Serial form. Each byte of Keystroke Data is clocked into the interface circuit by a Clock Signal provided by the keyboard itself.

The Keyboard Interface Circuit in the PX/XT (the first generation 8 bit bus DOS computers) is quite different to that used in the AT type and all subsequent types of DOS computers. The interface used in the 8 bit bus computer used a serial in -parallel out shift register to accept the keystroke data and the interface is only single sided. The interface is uni-directional, the keyboard only talks to the computer and the computer does not talk back to the keyboard. The AT computer introduced a bi-directional interface, and used an 8042 microprocessor chip in the keyboard interface to accept the keystroke data, and to provide some of the processing of the data. The basic processes involved are the same for both types of interface.

What happens when you press a key on a PC keyboard ?

When a key on the keyboard is pressed, the keyboard processor determines a Scan Code for that key from its position in the key matrix. The Scan Code for each key represents the keys position on the keyboard, rather than a particular character. When the PC. keyboard has a key pressed it clocks a Scan Code representing that key, into the computers Keyboard Interface circuit on the system board. When the keyboard interface circuit has received 8 bits (XT keyboard) or 11 bits (all other DOS computers) of Key Stroke data, it generates a Hardware Interrupt on IRQ 1, to start the Keyboard Service Routine.

Note; - As mentioned above, the keyboard also generates release-codes when that key is released but we will discussed this later.

Interrupts and service routines

When the computers processor gets the hardware interrupt signal IRQ 1, via an 8259 IRQ controller chip, it runs the Interrupt Handling Routine to find out the interrupt number that caused the interrupt.

It them uses this number to find the vector, from the Interrupt Vector Table, that points to the Keyboard Service Routine. The Vector is the address of the start of the Keyboard Service Routine.

The Keyboard Service Routine

The processor loads the start address of this routine into it's program counter and then proceeds to run the Keyboard Service Routine. The Keyboard Service Routine takes the Scan Code from the Keyboard Interface Circuit and produces a Two Byte Code that it puts in a Keyboard Buffer area in RAM Memory.

Other routines within the operating system or applications read the Key Stroke Data from the keyboard Buffer and make use of it.

The Status Bytes

The Status Bytes keep track of the shift state keys. A number of keys on the keyboard do not produce characters but they do determine what a particular key stroke means. These keys include the two Shift keys, the Control, the Alt., Caps Lock, Num. Lock , Scroll Lock and the Insert Keys.

These keys effect two Status Bytes, stored in low RAM Memory, and these bytes keep track of the state of these keys.

When the Keyboard Service Routine accepts a Scan Code from the keyboard, it consults the two Status Bytes at 417 and 418 hex, to see how the states of these keys, effect the Scan Code and hence the data produced.

The bits of the status bytes are used thus.

Byte 417 hex Byte 418H hex
Bit 1=active, 0=inactive Bit Meaning
7 Insert state 7 1=Ins depressed
6 Cap's lock 6 1=Caps-lock depressed
5 Num lock 5 1=Num lock depressed
4 Scroll lock 4 1=Scroll lock depressed
3 Alternate 3 1= Cont-Num lock active
2 Control 2 1=Pcjr keyboard click active
1 Left shift 1 not used
0 Right shift 0 not used

Example; - The "A" Key, has a Scan Code of 1E hex and could be "A", "a", or "ALT A", depending on the status of the Status Bytes.

Important:- The shift state keys listed above, do not produce codes in the keyboard buffer, but they do effect the Status Bytes, setting and resetting the bits in the bytes that they control.

The data written by the Keyboard Service Routine

The Keyboard Service Routine generates two byte codes, that it places into the keyboard buffer. The keyboard buffer is 32 bytes of RAM from 41E hex to 43D hex (0041E to 0043D). This is a circular buffer and the pointer to the start address is kept in locations 41A hex and 41B hex and the pointer to the end address is in locations 41C hex and 41D hex.

The Two Byte codes take the following form.

Normal Characters

Special Characters

Example; - The "A" key has a scan-code of 1E hex.

Keys pressed Aux byte Main byte
A "big" upper case "A" will produce 1Ehex 41hex (see the ASCII character set)
A "little" lower case "a" will produce 1Ehex 61hex (see the ASCII character set)
An "ALT A" will produce 1Ehex 00hex (Special Key)

The Auxiliary Byte values for the Special Keys can be seen in the Auxiliary Byte values for the Special Keys

How do The AT and XT keyboard interfaces differ ?

The XT and all later DOS computers handle the problem of keyboard interfacing quite differently. Today the XT type interface is dead and the interface first used in the AT type computers keyboard interface in 1983, is used in all modern DOS computers.

Note:The keyboard from an old XT type computer may not be able to be used in a more modern PC Computer system. Many of the XT keyboards were XT type keyboards only. Some keyboards have an XT/AT interface switch but most modern keyboard can detect the type of interface in use automatically. The switch on the keyboard would be labeled XT/AT or 8088/80286.

Data is sent by the Keyboard to the Interface Circuit in Serial form.

Data is send from the keyboard to the interface circuit on the computers System board in serial form and this data is clocked into the interface one bit at a time by a clock signal that is also supplied by the keyboard. With the more modern type keyboard interface the data takes the form of an eleven bit word with, one Start Bit (a low) followed by eight Data Bits (least significant bit first) an Odd Parity Bit and a Stop Bit (a high).

The Data and Clock lines that run between the keyboard and the keyboard interface circuit are normally high logic, pulled up by "pull-up" resistors on the keyboard end. When the keyboard wishes to send keystroke data to the computers keyboard interface it puts the data one bit at a time on the Data Line and then pulls the Clock Line low for a short duration while holding each data bit steady for a short time. The data rate is typically about 10K bits per second.

In modern PC computers, the interface lines between the keyboard and the interface circuit are bi-directional, and the computer can send commands to the keyboard via the same Data and Clock lines. The computer is really boss of the show and if the computer does not wish to receive key stroke data from the keyboard at any time, it holds the Clock line from the keyboard low.

The keyboard checks the state of the Clock line before it tries to send data to the computer and so it can see if the computer is ready to receive key stroke data. If the keyboard finds the clock line low it can store up to 16 key-presses itself. It will then send these to the keyboard interface circuit when the computer releases it's control of the clock line.

When the computer wishes to send a command to the keyboard it forces the data line low and leaves the clock line high. When the keyboard see's these conditions it responds with the hex code FA. If the keyboard detects an error in the data transmitted to it by the computer it will respond with FE hex and this code requests a re-transmission of the code that was in error. Errors are detected via the parity bits that accompany the codes sent by the keyboard or the computer on the keyboard interface lines.

A Shift Register or a Microprocessor

In the XT type computer the keyboard interface circuit consists of a Serial In - Parallel Out Shift Register and some logic circuits to generate a Hardware Interrupt (IRQ1) when keypress data has been loaded into this shift register. the Interrupt Service Routine invoked by the IRQ1 then reads the keypress data from the shift register via an 8255 programmable I/O chip.

The AT type computer in 1983 introduced a more advanced keyboard interface circuit using an 8042 microprocessor chip. The data sent over the data line from the keyboard to the interface on the system board is an eleven bit word consisting of a start bit, eight data bits, a parity bit and a stop bit. This data is clocked into the keyboard interface by a clock signal supplied by the keyboard on a clock line between the keyboard and the interface circuit. The keyboard interface processor receives the serial data from the keyboard, checks the parity of the data, and presents the data at its output buffer. The interface circuit then generates an IRQ1 to call on the Keyboard Service Routine to take this data from the buffer, and to process it.

The Release Codes

Most key presses produce a Keycode that represents the keys position on the keyboard rather than and actual character. Two types of Keycodes are sent by the keyboard to the Keyboard interface circuit on the computers System board. The Keypress code (make code) is sent when a key is pressed and a Release code (break code) is sent when the key is released.

Modern DOS computer keyboards support three sets of Press and Release codes. The Release codes are used to operate the key repeat function (called Typematic by IBM), that produces multiple key strokes if a key is held down for more than half a second. This function can be disabled by applications if required. The delay and the rate at which keystrokes are produced are determined by default values stored in low memory addresses.

The Keyboards power up sequence

On power up the keyboard runs a self-test routine that checks the keyboards processor and interface and then sends the code AA to the computers keyboard interface, if the keyboard is functioning correctly, or FD if a fault is discovered.

Once the diagnostic is finished the keyboard starts scanning the key matrix and monitoring the two Data and Clock lines between the keyboard and the computers keyboard interface. At this point some keyboards send out a string of AA bytes and this is thought to be part of the keyboards way of discovering if it is connected to an XT or AT type computer. This string of AA's has even parity instead of odd parity and so when the computer responds with a "re-send command" the keyboard can identify the interface in use.

Keyboard terms

Tactile response - the key top provides some resistance to being depressed.
Audible response - the key produces a click or other sound when the key is pressed.
Soft-touch - the keyboard has very little tactile response. (often referred to as the wet feel)
Click = the keyboard produces a loud audible response when pressed

Do not confuse the operation of the keyboard interface and the Video System. There is no direct link between the two. The Video System reads data from the Video RAM and produces the Video Display from that. The data in the Video RAM is written there by the Operating System or an Application.

The sequence of events involved with the PC keyboard The Scan Codes for a PC Computer Keyboard The Auxiliary Byte values for the Special Keys

More technical details on the PC Keyboard Back to the opening index Book three index