PS2 KEYBOARD ...SEND A STRING WHEN

Hey im trying to print a complete string of text to the serial port that has been typed on the ps2 keyboard when i pressed enter.

im using the following libary

http://www.arduino.cc/playground/Main/PS2KeyboardExt2

the problem im having is to detect a CR , ( "ENTER") from the keyboard to send the text.

The libary has commards for other keys like backspace ect .. but i cant find varible for enter key.

BUT ... it does send enter when i print directly to the serial port.

So im thinking look for the enter byte ?

#define KBD_CLK_PIN  3
#define KBD_DATA_PIN 4

PS2Keyboard keyboard;

void setup() {
  keyboard.begin(KBD_DATA_PIN);

  Serial.begin(9600);
  delay(1000);
}

#define is_printable(c) (!(c&0x80))   // don't print if top bit is set

void loop() {

  if(keyboard.available()) {

    // reading the "extra" bits is optional
    byte   extra = keyboard.read_extra(); // must read extra before reading the character byte
    byte       c = keyboard.read();



    

    if      (c==PS2_KC_UP)   { // this is the commards for the libary but i cant find one for enter .
 Serial.print("up\n"); {              

    Serial.print(c);   // this prints everything to serial port including enter


}

Any ideas ?

any help would be great .

cheers
luke

The PS2_ constants are for the non-ascii keys, such as the left and right arrows. The enter key generates an ascii value. It's either 10 or 13 (one is the carriage return, the other is line feed. I can never keep them straight, and I don't have a book handy).

luke,

According to the code, it returns a newline character when the enter key is pressed. Your sketch should check for '\n', also known as linefeed, LF, or 0x0a.

Regards,

-Mike

thanks very much for your replies ..

Mike would you mind giving a simple example ?

cheers
luke

luke,

Glad to. You could write it this way:

if (c == '\n')
  Serial.print("Enter key pressed\n");

Regards,

-Mike

cheer Again , i did that and it works a treat . Many thanks.

but now i need to store the string :c and send it.

Any ideas how i can add the keyboard press to the prevous one to build words ?

im guessing it would be output = c + c but this doesnt work nor i know where to put it .

cheers

luke

luke,

Have a look at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257731439/8#8. It's designed for Serial input, but you could easily adapt it for keyboard input. Change the '\r' to '\n'.

As that example shows, you'll need to build a string character by character, then put a NUL character ('\0') after the last character. This makes it a NUL-terminated string, which is what the functions that work with strings expect.

You might also have a look at avr-libc: <string.h>: Strings which shows which functions you have available for manipulating strings.

Good luck!

-Mike