How many letters do you type in a day?
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!
``` 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
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. Computer-Engineering.org has done a great job of describing the electrical and physical specification of the PS2 protocol.
I'm not going to go over the PS2 protocol in depth—it's all spec'd out in the site listed above.
Decoding the basic key presses from a keyboard is simple enough: wait for the clock to start toggling and record the data bits.
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.