I decided to make use of the Keyboard HID driver in a proof-of-concept project. The project: connect a USB keyboard to the Arduino and have the Arduino transparently pass the key presses to the host PC. Not all that exciting, but its the first step on my actual project which is a bit of April Fools payback for a coworker.
Some brief background; two weeks ago I was happily working on my laptop at work using the USB keyboard attached to the docking station. I pulled up a form and looked at the keyboard to find the first key I wanted to press, a "n", hit it, and in the form a "m" appeared. Hmm. I deleted it and tried again hitting the "n" key - and another "m" appeared! Confused I hit the "m" key and an "n" appeared. Arg!. I compared the keyboard's "n" and "m" keys to the laptop keyboard and discovered the cause - some prankster had switched the physical "n" and "m" keys on my keyboard. I'd been touch-typing all day without noticing the switch, it was only when I looked for the key that I discovered it. It was easy to work out who the culprit was, especially after he pulled the same stunt on a few more coworkers (let's call him Tony).
How does this relate to the Arduino? Basically I thought if I could get the Keyboard HID driver working so the Arduino looked like a keyboard to the host PC, and if I could also connect a USB keyboard to the Arduino, then I could install the Arduino between my coworkers keyboard and his PC while he was at lunch and indulge in some subtle or not so subtle payback. Hopefully on April 1st. For example, I could physically swap the "n" and "m" keys on his keyboard and have the Arduino also swap the "n" and "m" keys as he types them. Then when Tony touch-types his "n" and "m" key presses will be swapped, but when he looks at the keyboard and hits the "n" and "m" the correct key press will be sent to the host PC. Or, I could have the Arduino substitute every 50th key press with a nearby key, e.g. hitting "d" sends a "f" or an "s" instead. You get the idea.
I purchased a USB Host shield from Sparkfun
. This will provide the interface to the USB keyboard. I downloaded the library from Circuits At Home
and wrote a sketch based on the examples they provide. The sketch waits for input from the keyboard and then sends it to the host via the serial port. I loaded the sketch, flashed the Keyboard HID firmware to the atmega8u2, unplugged the USB cable, plugged a USB keyboard into the USB Host Shield, and plugged the UNO back in to my PC. It works! Its as if the Arduino is not even there, the keyboard works perfectly normally, no lag, key repeats work, numlock, capslock, scroll lock, all working really well.
I did have to cheat a little and handle the keyboard LED status in the sketch rather than taking the status from the host PC. I still need to debug that feature in the Keyboard HID driver.
Here's the keyboard pass-through sketch:
You'll need to use DFU mode to flash Arduino-keyboard-0.2.hex to the UNO's atmega8u2. See my earlier posts for the hex file and steps to follow to flash it to your board.