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.
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.
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.
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).
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.