How keyboards work

Welcome to the Matrix

The green letters that make up the virtual world in the Hollywood trilogy The Matrix have become legendary. But what does The Matrix have in common with PC keyboards, apart from the fact that you cannot write programming code without a keyboard, unless you are an AI?

The connection to The Matrix is ​​revealed when a keyboard is opened. The individual keys are arranged on a matrix of horizontal and vertical conductor paths. As soon as a key is pressed, a contact is established between one horizontal and one vertical conductor, closing a circuit. This is registered by a micro-controller, which then sends the scan code of the pressed key to the computer.

Keyboards, which use printed foils for registering the key actuation are especially well suited to reveal this matrix structure. Here, the rows and columns respectively are arranged on a printed polyester foil, with an insulating foil between the two. The actuation of a key, of which the resistance is usually provided by a silicone mat with small domes, happens with the contact of the two conductor foils underneath the button. At this point the circuit is closed.

Eine ältere Cherry-Tastatur mit bedruckten Folien als Schaltelement. Die Silikonmatte dient als Federelement für die einzelnen Tasten.
An older CHERRY keyboard with printed foils as switching elements. The silicone mat serves as a spring element for the individual keys.

In the case of keyboards with electromechanical switches, such as the already legendary MX modules from CHERRY, the switches are located on a circuit board with through-holes, but the conductor paths on the circuit board are also arranged in the form of a matrix.

Das Innere von CHERRY MX Board 3.0 Tastaturen
The back of a CHERRY MX 3.0 keyboard. The individual switches are mounted on a PCB. The upper edge reveals the micro-controller. It has only 48 pins.

The digital matrix is ​​used because it works with a lot less data lines, than if each key was individually connected, which would require far more than a hundred connections and a digital controller with a corresponding number of pins. Both would be complicated and expensive. With the matrix method, 24 pins can be sufficient for pure recognition of up to 128 keys with a matrix consisting of eight rows and 16 columns. Additional pins are required for the communication with the computer and other functions, such as controlling status LEDs.

One after another

To register a key stroke, the controller in a keyboard always queries the columns of the matrix first. It can either permanently scan all columns in sequence (polling), or the pressing of a key via a column interrupt wakes the controller, which then begins its work. The detection of an actuated key is always done by means of polling: If the relevant column is identified, the rows are scanned next until the relevant key is identified.

Der schematische Aufbau einer Tastatur. Die Tasten sitzen über Kreuzungspunkten von horizontalen und vertikalen Leiterbahnen in einer Matrix. Ein Tastendruck schaltet zwei Leiterbahnen kurz, der Controller registriert die Taste über die horizontale und vertikale Koordinate.
The schematic structure of a keyboard. The keys are located in a matrix at cross points of horizontal and vertical tracks. A key stroke briefly activates two conductor paths. The controller registers the respective key via horizontal and vertical coordinates.

The registration of a key via polling or column interrupt has nothing to do with the interface of the micro-controller but with its internal function. Column interrupt can also be used for keyboards with a USB interface. The keyboard controller’s communication with the computer, on the other hand, with USB interface keyboards always works by polling, whereas PS/2 input devices use an interrupt. In practice PS/2 keyboards do not have noticeable advantages in latency. Because the circuits are designed so that the controller can go into a suspend mode when no keys are pressed, the power consumption of USB keyboards is not higher either.

The scan code of the keys is transferred via the PS/2 interface, via USB or wireless to the computer and evaluated by the operating system. Basically, only the position of the keys is transmitted. PS/2 and USB keyboards use different code sets here. The translation of the scan codes into an ANSI character does not happen until they arrive in the keyboard driver. This allows the user of the computer to quickly switch between different character sets.

Witching Hour of the keyboards

The matrix method for key recognition works very effectively, but not without faults. A problem especially affecting computer gamers is the so-called ghosting. In this case, adjacent keys are no longer clearly identified when operated simultaneously.

Two simultaneously activated keys on different rows and columns are generally unproblematic. Even with more than two keys, the controller usually does not have any difficulty with the distinction, as long as they are either in one row or one column. Difficulties only occur when three keys in one block are operated at the same time. In this case the controller registers two columns and then two rows. This results in four possible keys that could have closed the circuit. Based on the resulting pattern, the chip can no longer determine which these are. In practice, for example, with the keys S, D and C held simultaneously, the X could still appear as a “ghost”.

The controller can handle two simultaneously pressed keys without problems, even if they are on the same row or column (right).

Bei drei gleichzeitig gedrückten Tasten in einem Viererblock tritt Ghosting auf. In unserem Beispiel registriert der Controller die Taste X als gedrückt, obwohl nur die Tasten S, D und C gehalten werden.
Ghosting occurs when three buttons are pressed simultaneously in a block of four. In our example, the controller registers the X key as pressed even though only the keys S, D, and C are held down.

With some keyboards, the conductor paths to commonly-used keys, such as W, S, D, and A, are therefore routed so that they are not arranged in one block. For expensive gaming keyboards, which are oftentimes equipped with MX switches from CHERRY, ghosting is largely prevented by additional conductor tracks. However, this effect can only be eliminated entirely by a completely different recognition technology, which works without a digital matrix.

Not related to ghosting is the limitation of a maximum of ten simultaneously detected keys with USB keyboards (six letters and four shift keys). This detection, also referred to as 6-key rollover, has been defined for the legacy mode on USB keyboards. It is used to make the keyboard appear to be a PS/2 variant to the BIOS during the boot process. There is no limit on PS/2 keyboards with N-Key Rollover (NKRO). In most cases, the keyboard controller limits the number of simultaneously detected keys. On the other hand, there are now also USB keyboards with controllers, which support NKRO.