UBM Tech
UBM Tech

How many letters do you type in a day?

Nathan Seidle, SparkFun Electronics, Inc -November 30, 2012

I wonder how many letters I type in a day. Is it 1000 key presses? 10,000? Or is it something horrendous like a quarter million? Just how much of my life is given up to key pounding? There was no good way to tell. I could try to write a VB app of sorts to monitor key presses but then I have to deal with the operating system and whether or not the OS is really reporting all the key presses (shift is a key after all). It's probably a trivial exercise under Linux, but I'm no Linux guru—I'm a hardware person.

So instead I settled on creating a hardware device that could sit in between the keyboard and the computer and count the key signals going back and forth. It was quite the learning experience!

Just an ATmega168 in between two mini DIN 6 connectors. You can get a Key Counter here.

``` Key presses today: 000001298

Assuming I can get the hardware to work, I will have a device that can listen to and manipulate PS/2 commands. What can one do with such a device? The applications are actually pretty wide. Of course I could try to turn it into a keyboard key logger and use it maliciously, but that's not really that fun.

What if we could change every 't' to 'T'? That would be really annoying (and fun to do to someone else). To manipulate a 't' like that, we cannot just listen, we have to listen and then change the data. A logger can't do that. But the Key-Counter can!

What if you needed to re-route all the QWERTY commands to a Dvorak setup? This hardware could do it without a single driver or OS requirement!

What about an algorithm that predicts what you're trying to spell? Just like predictive text on your cell phone, the Key-Counter could try to suggest words as you're typing them.

But wait—if I can receive keys from the keyboard and send them to the computer (and have text display on the screen), can I not use the sending of various keys to the computer as debug statements? I could have an entire menu pop up without having to open a terminal window or other software. Anywhere I can type, I can get the Key-Counter to display its information.

 ``` Key presses today: 000003413

Rather than using a UART and a troublesome RS232 circuit and terminal window, I could create a board that did analog to digital conversions and then report those values over the PS2 port. This board could effectively type the data straight into a spreadsheet. Nifty.

Once you have control of the PS2 commands zipping back and forth, the ideas start to flow. For now, let's just try to see how many keys are pounded in a day.

``` Key presses today: 000005866


PS/2 Protocol

Where does one find out information about how the keyboard communicates with the computer? Google of course. A quick search of 'ps2 protocol' brings up the holy grail of PS2 information. has done a great job of describing the electrical and physical specification of the PS2 protocol.

This is the Key-Counter under interrogation. You can see the purple keyboard on one end, the white PS2 cable going to the computer, the gray and red AVR-PG2 programming cable (with 10-pin to 6-pin converter PCB). I also have 5 wires connected to my LogicPort logic analyzer.

Here is the Key-Counter layout

The electrical connections between the keyboard and computer were relatively straight forward.

I'm not going to go over the PS2 protocol in depth—it's all spec'd out in the site listed above.

Here is the 'q' key being pressed.

Decoding the basic key presses from a keyboard is simple enough: wait for the clock to start toggling and record the data bits.

This is the 'q' key being released.

Whatever is heard coming from the keyboard (0xF0 followed by 0x15), the Key-Counter should pass it on to the computer.

Notice how the K-Clock line is pulled low for nearly 1ms after we receive 0xF0 from the keyboard. This is the Key-Counter pulling the K-Clock low. While the Key-Counter holds K-Clock low, it sends out the command to the computer. By holding K-Clock low, the Key-Counter is telling the keyboard that it (the computer) is busy and should not send any commands until the clock is released. Otherwise, we've found that newer keyboards send out commands with as little as 10µS in between commands. Since the Key-Counter doesn't buffer or handle interrupts, we have to tell the keyboard to sit tight for a second while we pass the last key press out to the computer.
Next: page 2

Loading comments...

Write a Comment

To comment please Log In