Hacking a Real Keyboard
Remember I said you might get to take something apart? Im sure I heard someone laughing with glee when I mentioned that. Well, nows the time! Probably the least expensive way to connect arcade controls to your computer is by hacking apart a keyboard. (Several keyboards were harmed during the creation of this book.) Ill start by taking a look at how keyboards work and then proceed into turning one into an arcade interface. How keyboards workThe actual physical makeup of a keyboard can vary quite a bit. A typical design includes either a circuit board or a flimsy material (hereafter simply flimsy) that lies underneath the buttons on the keyboard. Laid out on the flimsy is a maze of circuits. Directly beneath each keyboard button on the flimsy are two halves of a circuit. The underside of the button has a conductive material of some kind. When the button is pressed, the conductive material comes into contact with the two halves of the circuit below, completing the circuit. Variations on this design exist, but almost all are similar. Take a look at Figure 8-1 for an example.
How the keyboard operates should sound very familiar. Its just like the arcade pushbutton coming down to press the microswitch button, completing that circuit. The only piece that the keyboard has that is missing in the arcade pushbutton circuit is the keyboard encoder. Could we use the encoder from a real keyboard for our purposes? Take a look at the keyboard encoders shown in Figure 8-2.
Notice the connectors at the edge of the keyboard encoder boards. Those stripes are the contacts that the circuits on the flimsy come back to. If you start counting, youll realize that there arent nearly enough contacts to account for all the keys on the keyboard. Even if every keys circuit used a shared common ground, there are still not enough contacts to account for the 100 or so keys found on a typical keyboard. Whats going on?
If a keyboard was configured to use discrete contacts (one contact per key), there would be over 100 contacts required and a keyboard encoder chip with the same number of pins on it! That would be big and expensivesomething manufacturers always try to avoid. Instead, keyboard makers take advantage of a design technique called a matrix. No, Keanu Reeves is not going to show up suddenly in your arcade cabinet. A matrix is a method of using a small number of contacts to account for a larger number of inputs by arranging them into a grid. Take a closer look at the keyboard encoder in Figure 8-3, to notice the contacts are separated into two groups. There are 14 contacts on the left, and 8 on the right.
If the keyboard was setup in discrete mode, there would only be 22 buttons possible on the encoder shown in Figure 8-3, because there are only 22 contacts. However, this encoder is configured in a grid of 14 x 8. This gives a total of 112 possible buttons (14 x 8 = 112) more than enough for a standard 104-button keyboard. The way it works is that the keyboard encoder has a map of this matrix programmed in its memory. Take a look at the matrix in Figure 8-4. Ill refer to the 14-contact side of the matrix as the x side and the 8-contact side as the y side.
The "A" button is in the first spot on the matrix (X1-Y1). When the "A" button is pressed, the encoder sees the circuit that connects the first contact on the 14-contact side (X1) and the first contact on the 8-contact side (Y1) (see Figure 8-5). The encoder looks this up in its map and generates an "A" keystroke.
Determining which keyboard button has been pressed is a simple matter of the encoder doing a lookup in its table for the X and Y value that is generated by the key press. There is no set standard for how a matrix is designed and laid out. Another keyboard manufacturer might configure its matrix to be 13 x 9, allowing for 117 buttons, with the keystrokes occupying different spots on the grid.
What do keyboards do? In simple terms, a keyboard allows you to press a key, which closes a circuit associated with a particular keystroke. That closed circuit is detected by a mini-computer inside the keyboard called a keyboard encoder, which recognizes which key has been pressed. The keyboard encoder takes that information, encodes it in a digital form the main computer can understand, and passes it to the computer via the keyboard port.