PS/2 key logger

I’ve been trying for about a week now to make a simple PS/2 keyboard keylogger and I’m so close but its still not working. I’m connecting the keyboard to the Arduino by simply tapping into the line and splicing the clock and data wires with ports 3 and 2 respectively. If I also splice the power and ground and connect everything to the Arduino (the other end not connected to the computer) the logger works perfectly. But once I use the power from the actual line and connect it to the Computer the data comes out as incorrect characters and symbols. I have no idea why this would happen but it probably has something to do with data now passing to the computer (the computer displayed data is correct by the way). Any suggestions would be helpful, thanks!

#include <PS2Keyboard.h>

const int DataPin = 2;
const int IRQpin =  3; 

//clock pin 3, data pin 2, ground and 5v connected
PS2Keyboard keyboard;

void setup()
{
  Serial.begin(9600);
  keyboard.begin(DataPin, IRQpin);                                                
  Serial.println("Keyboard Test:"); 
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 'x')
    {
     Serial.println("Test");
     Serial.println("");
    }
  }
  
  if(keyboard.available())  
  {
    char c = keyboard.read();
    Serial.print(c);
  }  
  else;
}
#include <PS2Keyboard.h>

const int DataPin = 2;
const int IRQpin =  3; 

//clock pin 3, data pin 2, ground and 5v connected
PS2Keyboard keyboard;

void setup()
{
  Serial.begin(9600);
  keyboard.begin(DataPin, IRQpin);                                                
  Serial.println("Keyboard Test:"); 
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 'x')
    {
     Serial.println("Test");
    }
  }
  
  if(keyboard.available())  
  {
    char c = keyboard.read();
    Serial.print(c);
  }  
  else;
}

If you power the keyboard from USB, is the voltage (5V) okay?

(deleted)

DrDiettrich: If you power the keyboard from USB, is the voltage (5V) okay?

the keyboard is being plugged into the ps/2 slot as it is a ps/2 keyboard. There shouldn't be an voltage issues, at least I don't think so, as the Arduino is only tapping into the data and clock wires.

spycatcher2k: and you do have the grounds still connected ? yes?

No I'm only tapping into the clock and data wires, when I connected all four wires (including ground and 5V) to the Arduino, it works perfectly(with the other end not connected to the computer).

Now I understand your circuit better. You may have a synchronization problem, like inverted clock/data signal levels.

Verify that you receive the same codes, when pressing the same key. Then try to match the received codes with the expected codes.

DrDiettrich: Now I understand your circuit better. You may have a synchronization problem, like inverted clock/data signal levels.

Verify that you receive the same codes, when pressing the same key. Then try to match the received codes with the expected codes.

That's the thing, i tried doing this but i get a different key every time. So if I continuously hit the "a" key on the keyboard, I will get many different characters on the serial monitor. Could it be that my serial monitor is Bluetooth? Could that somehow mess up the data?

I had the exact same problem and found a solution. Solved it by putting 1K ohm resistors in series with pin 1 and 5 at the ps/2-connector on the computer end. So resistance between keyboard and computer is 1K ohm for data and clock, but 0 ohm between keyboard and arduino.

I also pulled up the arduino data and clock inputs with 10K ohm to +5V, but this alone did not help.