Thursday, January 1st, 2009

Adapting a toggle switch to a ‘keyboard hack’ (toggle on/off to pulse)




Probably one of the most common questions I’ve received via comments, PMs and e-mail is ‘so how to you use standard toggle switches with a keyboard hack’?

I won’t go into too much detail about the keyboard hack here (and there are plenty of other websites that described it in detail), but the basic principle is that you find an old keyboard and remove the controller chip inside. When you press a key on the keyboard, the key squishes together two conductive surfaces printed onto layers of plastic. These conductive lawyers are arranged in lines across a grid-like pattern. Each key corresponds to a unique set of coordinates in this grid. For example, pushing down the ‘A’ key might have the coordinates A1-B3 corresponding to the A1 and B3 pins on the controller chip.

There are generally three different ways to figure out the ‘map’ for the controller chip (eg which combination of coordinates equals which keys).

– Search the web and see if someone else has mapped the same chip (long shot)

– Carefully trace the the lines on the conductive layers to figure out each key’s coordinates (possible, but likely to give you a huge headache)

– Test out each combination of pins by systematically crossing each combination from the two sets of pins

The third method is probably the quickest and easiest. Generally I will just make a grid pattern (with the top and side corresponding to the two sets of pins on the chip). Small wires can be placed onto the pins and then quickly tapped together to simulate the pressing of a key. If you have a word processing program open, you’ll quickly be able to see which pin combinations correspond to which letters. Some, such as ‘shift’ or ‘ctrl’ won’t be obvious, but just ignore any combinations that don’t produce a letter or number response (as you generally won’t need that many different keys to map).

IMPORTANT NOTE: If possible I’d recommend that you try this ‘testing’ procedure and the testing of any new ‘keyboard hack’ setup on an older, or less important, computer as there’s always the small possibility that you could short something out and cause damage to the motherboard. This is quite unlikely with a simple keyboard hack without any external voltage. However, the sections below describe procedures that do use external voltage that very much could damage your computer if you’re not careful about how circuits are constructed.

Once you have the keyboard controller all mapped out, it’s quite simple to wire up the controller to a bunch of ‘push’ switches. Pushing the switch is then essentially like pressing the key as it creates a momentary connection that the controller interprets as a single keystroke.

However, this setup won’t work for the more common ‘on/off’ style switches. When the switch is turned ‘on’ if forms a constant connection making the controller think that the key has been held down. The controller then simply sends a continuous string of the same letter into the PC. Equally, when you turn the switch ‘off’ the connection is now lost and this string of text simply stops, rather than sending another simple signal indicating that the switch was turned off.

The trick here is that you need to get these ‘on/off’ style switches to send a quick pulse to the controller each time the switch is turn on or off, but not send any signal when the switch is just sitting in the on or off position.

The solution involves creating two circuits both connected through a relay (see end links for diagrams). The first circuit passes through the ‘on/off’ switch and connects a capacitor to an external voltage source. When switched on, current flows through this circuit and into the capacitor, but only until the capacitor is charged. If the right voltage-capacitance combination is used, this process will only take a fraction of a second.

The relay acts a switch and that closes a circuit across one set of pins when current flows through a different set of pins. In this case, wires connected to the keyboard controller are passed across the ‘switch’ set of pins. When the toggle switch is turned on, the pulse of current (while the capacitor charges) will cause the relay’s switch to momentarily close the connection between the keyboard controller leads and, hopefully, cause the controller to register a single keystroke.

When the toggle switch is turned off, the capacitor circuit now disconnects from the external voltage source and the capacitor quickly drains its stored charge. Again, the relay detects this flow of current and momentary closes the connection to the keyboard controller to register another single keystroke.

The key here is that the pulse of current from the capacitor circuit must close the controller circuit long enough to register a keystroke, but not too long as to register multiple keystrokes.

In my own setup, I used a 9V battery, 47 micro-farad capacitor and a 0.5-Amp Reed 5V Reed Relay.

This arrangement worked will with my controller (and I imagine it will work well with most controllers); however, you may need to adjust these parameters to increase or decrease the pulse time. If the pulse is too short you can increase it by using a larger capacitor and similarly you can decrease the pulse time by using a smaller capacitor.

All of these components are available at Radio Shack for a few dollars each; however, you can purchase the same items from online electronics suppliers for a tiny fraction of the cost. The same goes for all components, especially LEDs! Therefore, I’d recommend going to Radio Shack to buy one set of components to test out your circuit design and then order in bulk from a less expensive supplier.

Another word of warning: Be very careful when putting these circuits together. Although there’s no risk that you’ll shock yourself you very much could ‘shock’ your computer. The role of the relay is to separate the ‘brute force’ capacitor circuit from the ‘delicate’ controller interface circuit. However, if you connect the circuit incorrectly you could drive the external voltage right into your keyboard controller frying it (bummer) or your motherboard (real big bummer). So long as everything is wired up correctly this should not be a problem, but just be extra careful.

If possible, it’s best to test out your circuits with a conductivity/voltage detector connected to the keyboard controller side of the relay. If it’s working correctly, the pins designated for connection to the controller should register no voltage when the switch is flipped and a very quick blip of conductivity. Afterall, the relay should just be momentary closing the connection between wires leading to the controller chip.

If you’re not used to building circuits then the first time can be a bit tricky, but with a bit of patience and persistence it’s not too hard to get one of these setups up and running.

Finally, for the additional details needed to put one of these setups together here are links to two other examples of these circuits with diagrams of the necessary circuits (sorry I was too lazy to draw my own diagrams… maybe later 😉 ).

Additional articles to check out (with diagrams):

http://www.simprojects.nl/toggle_to_momentary.htm

http://home.planet.nl/~schre010/fs/toggle.html

Sponsored video content:



6 Comments on “Adapting a toggle switch to a ‘keyboard hack’ (toggle on/off to pulse)”


  1. Oh, in case anyone wonders I ordered my components in bulk from Mouser Electronics.

  2. David

    Thanks for the tips on toggles. I found a much cheaper and simpler solution. I simply installed three-way toggle switches and wired the keyboard button to the centre position of the toggle only. The top and bottom are both null. So by toggling up or down, you pass through the centre, causing a pulse, just like pressing a button. Works well, though not a pure way of doing it.


  3. Thanks for the tip David. This is certainly a potential alternative. As you point out, this won’t result in the true ‘on/off’ type toggle switch that some folks want… but it so much simpler to build and wire together that I imagine most folks won’t mind ;-).

  4. karl

    Instead of having to do all the wiring etc, there is a program called SVMapper which changes the toggle to momentary in software.


  5. SVmapper can only really be used with USB devices to change a button click INTO a keystroke. The keyboard hack is a good soloution to a simple problem.

  6. rico

    im having trouble with my toggle switches i have a 9v battery hooked up to the switch and i have the positive side of my capacitor hooked to the ACC {what does ACC mean?} part of the switch and i have my reed relay hooked to the ground part (my relay only has 4 prongs) and the negative side of my capacitor is hooked to the relay and when i put the 2 wires from my ps/2 keyboard it types the correct letter but does not stop even when the switch is turned off or on

    relay:0.5 amp\5vdc
    capacitor:35vdc\470uf
    20 gauge wire

Post comments, questions and suggestions here:

You must be logged in to post a comment.