Difficulties with Keyboard Hacks
I find the physical work of soldering the wiring to the keyboard encoder challenging, mostly due to the tight quarters. However, physical challenges aside, there are other issues with keyboard hacks. Ill cover each one of them briefly. Ghosts in the machine
This only occurs when three keys in the corners of a rectangular area of a matrix are pressed simultaneously. Typically this does not occur when a keyboard is used for typing. However, this can occur very easily when used for arcade controls. Ghosting could be a problem if, for example, your "up-right" (diagonal movement involving two microswitches) and "jump" keys generated a ghost "quit" key. Picture poor Mario running his heart out, about to jump over one last barrel to save the damsel in distress, only to have the whole game suddenly exitleaving Mario and his lady in lovers limbo forever. That would be enough to make any pixelated persona pack up his bags and go into a less stressful line of work, such as shark dentistry.
Ghostbusting with blocks, design, and diodes
Three solutions present themselves to the ghosting problem. The first is to simply purchase a new keyboard. Ghosting is a trait of old keyboards from a few years back. Newer keyboards usually have logic designed into the encoders to block ghost keys from appearing. Recognizing when two corner keystrokes of a rectangle in the matrix have been generated, the encoder will simply block the third corner keystroke from appearing. This is known as keyboard blocking, and has its own drawbacks. There may be times when you want those three keystrokes to be able to function simultaneously. Id recommend against it, but you may not have a choice depending on the encoder matrix and requirements of your software. No ready solution is available for keyboard blocking other than to try a different keystroke combination or different keyboard encoder.
The second solution is to select the keystrokes that you are going to use from the matrix such that three corner keystrokes from a rectangle in the matrix cannot be generated. Examine your matrix map, and make sure that no three keystrokes form a rectangle with a fourth keystroke that will cause you problems. This prevents ghosting and blocking both. This can be a problem if your software requires the use of three keystrokes that violates the above rule. However, software that does not allow changing its controls is fairly rare. Another possibility is to choose keystrokes that are impossible to have occur simultaneously. For instance, the keystrokes you choose for up, left, and right could safely be chosen even if they would normally be candidates for causing ghosting, because its impossible to move a joystick in all three directions at the same time. With careful planning, ghosting and blocking issues are a moot point.
The final solution brings up another electrical component the diode. A diode is a device that in simplest terms only allows electricity to flow in one direction. A diode has two ends: a cathode (-) and an anode (+). Current can only flow from the anode to the cathode, but not the other direction (see Figure 8-8).
Ghosting is an electrical issue. With proper diode placement, you can prevent electricity from flowing the wrong way on the matrix, preventing a ghosting situation. The following is paraphrased from information provided by the folks at Hagstrom Electronics, a keyboard encoder vendor whose products are introduced later in this chapter.
While scanning the keyboard matrix for activity, a keyboard encoder will normally check columns in the matrix one at a time. When it activates a column, it will check each row to see if any circuits are completed for the current active column. It then moves on to the next column. When three or more switches are pressed that share two columns and one row (or two rows and one column) then electrical ghosting occurs. In many early PC keyboards, you will find a diode in series with each button of the keyboard. On these keyboards, you may press every key on the keyboard at the same time, and the keyboard sends all that information to the PC. By placing a diode in series with each switch, ghosting is prevented. The cathode of the diode is connected to the column side of your matrix, the anode is connected to your switch, and the other contact on your switch connects to the row (see Figure 8-9). Using isolation diodes in a matrix to prevent ghosting is a technique that has been used in keyboards for many yearseven preceding the personal computer. Cherry Corporation, a popular manufacturer of keyboard switches, even offers an option of built-in diodes on their switches.
Is your head spinning a bit? Dont worry about the theory. If youre suffering from ghosting problems, pick up some 1N4148 or equivalent diodes (a good electronics store will know what to substitute), and connect them up to the switches having problems. One of two things will happen. Either your ghosting problem will go away, or the switch will stop working entirely. If it has stopped working altogether, your diode is in backwards. Reverse it, and all is well.
Keyboard hacks can suffer from ghosting problems. Keyboards can be haunted? In this case, I am not referring to any nether-worldly spirits. Ghosting is a potential side affect of having a matrix design. What can occur in older keyboards is when three keys are pressed, a phantom fourth keypress is detected by the encoder even though no physical key was pressed. Figure 8-7 shows why this can happen. Recall that a keyboard encoder works by detecting completed circuits. When you press "A," circuit X1-Y1 is completed. While pressing "A," also press "B" to complete circuits X1-Y1 and X1-Y2. Add "O" and something interesting happens. Not only have you added circuit X2-Y1, but because the three keys "A," "B," and "O" involve all four terminal points, there are complete circuits between all combinations including X2-Y2, even though no key was physically pressed at X2-Y2. The encoder cannot tell that circuit X2-Y2 wasnt intended to be completed and so generates a phantom "P" keystroke.