General

PS2KeyboardExt is an extension of the original PS2Keyboard library. It works on top of the original library to provide the following additional features:

  • Support for letter, punctuation and additional function keys
  • Backspace
  • Limited "shift" support through caps lock (see Functionality for more information)
  • Additional functions for dealing with input

The goal of this extension is to add support for as many keyboard functions and keys as possible while keeping the library simple and relatively small.

Currently, the example code below produces a hex file about 3.7kb, which is a little on the larger side, but hopefully the code will get smaller over time. This extension was intentionally kept separate from the original library for those who don't need all these features and would like to save program space.

Usage

If you haven't already, check PS2Keyboard out and download it. Then, download PS2KeyboardExt.zip below and extract the contents into the same directory as the other PS2Keyboard files (e.g. ~/arduino-0011/hardware/libraries/PS2Keyboard). Here is some example code for this extension:

#include <PS2Keyboard.h>
#include <PS2KeyboardExt.cpp>

#define DATA_PIN 4
#define capslkLed 13                             // the capslock LED pin

PS2Keyboard keyboard;

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

  pinMode(capslkLed, OUTPUT);

  Serial.begin(9600);
  Serial.println("Hi :)\n");
  delay(1000);
}

void loop() {
  if(keyboard.available()) {
    byte dat = keyboard.read();                   // read one byte from the keyboard
    byte datType = getKeyType(dat);               // get the type of button that was pressed
                                                  // (function, number, letter or punctuation)
                                                  // will return 1, 2, 3, or 4

    if (datType == 1)                             // type came back as a function
    {
      byte funct = getKeyFunct(dat);              //   so get function number
      if (funct == 1) {                           //   function number came back as 1 for 'return'
        Serial.println();                         //     so print a new line
      } else if (funct == 2) {                    //   function number came back as 2 for 'space'
        Serial.print(' ');                        //     so print a space
      } else if (funct == 3) {                    //   function number came back as 3 for 'tab'
        for (int i = 0; i < 6; i++) {             //     so print 5 consecutive spaces
          Serial.print(' ');
        }
      } else if (funct == 4) {                    //   function number came back as 4 for 'backspace'
        Serial.print(8, BYTE);                    //     so go back one character
        Serial.print(" ");                        //     print a space over that character
        Serial.print(8, BYTE);                    //     and go back one character again
      } else if (funct == 5) {                    //   function number came back as 5 for 'capslock'
        if (capslk == 0)                          //      so invert the value of capslk and its LED
        {
          capslk = 1;
          digitalWrite(capslkLed, HIGH);
        } else {
          capslk = 0;
          digitalWrite(capslkLed, LOW);
        }
      } else if (funct == 6) {                    //   function number came back as 6 for 'escape'
        Serial.print("\[ESC]");                   //     so print '[ESC]'
      }
    } else {                                      // type came back as not a function
      char datChar = getKeyChar(dat, datType);    //   so get character the user was trying to type
      Serial.print(datChar);                      //   and print it
    }
  }
}

Functionality

PS2KeyboardExt.cpp is heavily commented, but here's a thorough explanation of how this library extension works.

As mentioned above, PS2KeyboardExt supports many more characters than just 0-9. More specifically, it supports all the letter keys, digit keys, punctuation keys, as well as their "shift" character (e.g. 'm' and 'M', '5' and '%'), and six function keys (return, space, tab, backspace and capslock). The "shift" character of a key is received from the keyboard when capslock is on. Capslock is toggled with the button on the keyboard as usual, but it will light the LED conncected to capslkLed as opposed to the LED on the keyboard. This is because lighting the keyboard requires sending commands to it, which neither this extension nor the original library support.

PS2KeyboardExt stores all these characters in lookup tables. It has a lookup table for each type of key: functions, numbers, letters and punctuations. These tables contain bytes the keyboard sends for specific keys, and their corresponding character and "shift" character. The functions table works a little differently; see getKeyFunct()'s description for details.

PS2KeyboardExt provides these functions:

  • byte getKeyType(byte keyByte)
  • char getKeyChar(byte keyByte, byte keyType)
  • byte getKeyFunct(byte keyByte)

byte getKeyType(byte keyByte) - reads a keyboard-sent byte and searches the lookup tables for it. Depending on which one it found the byte in, it will return a number from 1 to 4 (function, number, letter or punctuation). If it isn't found in any lookup table, it returns 0.

char getKeyChar(byte keyByte, byte keyType) - searches the lookup table of type keyType for keyByte. If found, it will return either the corresponding character or "shift" character, depending on whether capslock is on or off(if capslk = 1 or capslk = 0). If not found, it will return 0.

byte getKeyFunct(byte keyByte) - searches the function keys lookup table for keyByte and returns its position in the lookup table, starting from 1. If the byte isn't found, it will return 0.

Download

Attach:PS2KeyboardExt.zip

Further Readings

Suggestions

Share