Altering Functionality of PS/2 Keyboard

So wiring up the keyboard and reading using the PS/2 Keyboard Library was dead simple, but now I'd like to do something a little different. As it is now, when you press down a key, it performs like a usual keyboard, printing it's respective letter. When you hold it down, it prints the letter over and over, like thisssssssssssss. I would like instead to simply read when the key is pressed and when it is released.

Here's what I've got right now (hacked from the example supplied with the library).

void loop() {
  if (keyboard.available()) { //tells if a key is pressed
    
    char c = keyboard.read(); //finds out which key
    
    //Serial.print(c);
    if (c == PS2_ESC) { //is it the escape key that is pressed?
      if (current_key != 1) { //have we already established that it is pressed?
        current_key = 1; //set key as "on"
        Serial.print("[ESC ON]");
      }
    }
  }else{
    if (current_key != 0) { 
      current_key = 0; //set key as "off"
      Serial.print("[ESC OFF]");
    }
  }
}

But when I hold a key, it just repeats "[ESC ON][ESC OFF]" over and over again. Am I to deduce that keyboard.available() is only detecting a key press every other loop? If this is the case, is there a way to set up a delay so that they will be in sync? Or better yet, an easy way to fix this in the library files? Any thoughts would be much appreciated.

Figured it out, thanks to this post I found: http://nocommercialpotential.posterous.com/interfacing-the-ps2-keyboard

Thanks for posting that link, added to the PS/2 playground page with credits to you.

Oh, no problem.

I actually have a different question now about ps/2 scan codes. When I set it up to print the scan code in hexadecimal like
Serial.print(key, HEX);
For certain keys, it gives me two numbers comma separated. For example, the up arrow gives me "E0,75"... I consulted the scan code set and this is indeed correct, but I can't find anything about how to work with this data. When I set it up to print scan codes in binary, it gives me 11100000, which is equal to E0. So I just figured it was either/or, and set up my code to check for 0xE0.

This SORT OF works, but unlike the rest of the keys which I have set up to only detect the keypress only once when held, if I hold this key down it detects a keypress every loop ("[up][up][up][up]etc.."). I've tried checking for 0x75 as well, with the same results.

So.... what am I missing here?

The technique to parse is to "look one char ahead", so you must read one char, check if it indicates a special char, if so read another char

in pseudocode

byte ch = readCharPS2();
switch(ch)
{
  case 0xE0: // special char => 2 bytes
    ch = readCharPS2();
    processSpecial(ch);
    break;

  case 0xE1: // another 2 byte code (for example)
    ch = readCharPS2();
    processSpecial(ch);
    break;  

  default:  //normalchar
    process(ch);
    break;
}

In the same way you can process any special key.

Ahhhhh. I see. Thanks so much, I don't think I ever would have figured that out on my own.

Welcome, most of the problems you must start to tackle with pen and paper, not with programming :wink:

How do you connect the wires from a PS2 keyboard to the Arduino?

And will this interface be able to make LED displays like these work to say things like
"Merry Christmas!", "Happy Birthday!", etc.?


Here's my LED display I bought on eBay that I'm working on this project with.


And here's the terminal strip for additional help.

tpirman1982:
How do you connect the wires from a PS2 keyboard to the Arduino?

The Playground page for PS/2 Keyboard (also linked in the original post) has a wiring diagram showing how to connect the contacts on the MIDI plug to the appropriate pins on an Arduino.

As far as the LED display, if you don't have some documentation on how to connect "Data A" and "Data B" and what voltage levels they require it would very helpful to find some.

This link is dead. Can somebody post what it said?
http://nocommercialpotential.posterous.com/interfacing-the-ps2-keyboard

wb8nbs:
This link is dead. Can somebody post what it said?
http://nocommercialpotential.posterous.com/interfacing-the-ps2-keyboard

Not sure if you still need it.