Different hex code with PS2

Hi everyone

I tried to connect PS2 cable of keyboard to arduino UNO and get the data from keyboard. I have read many pages on internet and also used different libraries of Arduino. The problem is that non of them cannot detect (read) the "PrtSc" button. Then, I tried to write the code myself. This is the code:

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {  
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    
    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, HEX);
  }
 }

The problem is that when I pressed key "1" different times, it gives me different three hex values comparing together. Interestingly, "PrtSc" works correctly and return the same value always.

Any help is appreciated in advance

You're not reading from the keyboard, you're reading from the Serial port.
You just have to find the scan code for the PrtScr key, and a library that reads the scan codes from the PS/2 port (all libraries do this under the hood).

Pieter

Dear Pieter

Thank you for your reply.

PieterP:
You're not reading from the keyboard, you're reading from the Serial port.

The question is that why it return different valus for one key.

PieterP:
You just have to find the scan code for the PrtScr key, and a library that reads the scan codes from the PS/2 port (all libraries do this under the hood).

Do you mean that I have to find a code that able to scan the code corresponding to each key? Do you have any code?

Thanks

Try this library: GitHub - PaulStoffregen/PS2Keyboard: PS/2 Keyboard Library for Arduino
There are examples and documentation available. Instead of keyboard.read(), use keyboard.readScanCode(). If the result is not zero print it to the Serial monitor.