Keyboards use a matrix of wires, in rows and columns. Each key is a switch that connects a row to a column, where each key has it's own unique position, or address, in the matrix.
This is a very simple 4-key matrix. You won't ever see something this simple in a keyboard, but for our purposes it's more than enough.
To detect keypresses, the keyboard will scan column by column and check to see which rows have been activated. In the image below, when the keyboard activates C1, R1 goes hot and therefore it knows that A
has been pressed. When it activates C2, neither R1 nor R2 go hot so it knows that B
haven't been pressed.
Multiple key presses work in the same way. In this image you can see that when C1 is activated, R1 goes hot, giving the letter A
. Then when C2 is activated, R2 goes hot, giving the letter D
But the problem in this matrix shows up as soon as you press three keys at once. In this image A
, and D
are pressed. The B
switches short R1 with R2 because they are both closed; so when C1 is activated, both R1 and R2 go hot and the keyboard thinks that C
has been pressed, and sends it to the PC even though you didn't really press it. This is what's called a "ghost
There are two methods used to prevent ghosting. The first and cheaper option is for the controller to block that third keypress that causes the ghost key. So after pressing A
, it ignores both B
because pressing either one will cause the other to ghost. This gives this board 2-key rollover, because only 2 keys can be pressed at once.
The other option is to install a switching diode in series with each switch. The diodes only allow the current to flow in one direction, so the rows no longer get shorted to each other. In the image below you'll see the A
, and D
keys pressed again, but this time there are diodes to control the flow. Notice how R2 no longer goes hot when C1 is activated.
This method allows for each and every key on the board to be detected independently, giving it n
-key rollover (NKRO). It's called n
-key because n
is a variable, representing the number of keys on the keyboard.