| ||
Shopping Cart Download Website |
Home | Products | Teensy | Blog | Forum |
You are here: Teensy Teensyduino Libraries PS2Keyboard |
|
PS2Keyboard LibraryPS2Keyboard allows you to use a keyboard for user input.
Hardware RequirementsPS2 Keyboards are easy to interface, requiring only 5 volt power and 2 signals.
The keyboard's clock signal must connect to an interrupt pin. The data signal may connect to any pin, but do not use the pin with an LED connected to ground because the LED current will interfere with the data signal.
Caution: PS2 Keyboards contain pullup resistors to 5 volts which can cause damage to 3.3V only boards like Teensy LC and Teensy 3.6. Use of a level shifter circuit is needed to safely use boards which are not 5 volt tolerant. Basic UsagePS2Keyboard keyboard;Create the keyboard object. Even though you could create multiple objects, only a single PS2 keyboard is supported by this library. keyboard.begin(DataPin, IRQpin)Begin the receiving keystrokes. DataPin and IRQpin are the pin numbers where you connected the PS2 keyboard's Data and Clock signals. keyboard.available()Check if a keystroke has been received. Returns true if at least one keystroke. keyboard.read()Read the next keystroke. -1 is returned if no keystrokes have been received. Keystrokes are returned as ASCII characters. Special keys are mapped to control characters. Example Program
#include <PS2Keyboard.h> const int DataPin = 8; const int IRQpin = 5; PS2Keyboard keyboard; void setup() { delay(1000); keyboard.begin(DataPin, IRQpin); Serial.begin(9600); Serial.println("Keyboard Test:"); } void loop() { if (keyboard.available()) { // read the next key char c = keyboard.read(); // check for some of the special keys if (c == PS2_ENTER) { Serial.println(); } else if (c == PS2_TAB) { Serial.print("[Tab]"); } else if (c == PS2_ESC) { Serial.print("[ESC]"); } else if (c == PS2_PAGEDOWN) { Serial.print("[PgDn]"); } else if (c == PS2_PAGEUP) { Serial.print("[PgUp]"); } else if (c == PS2_LEFTARROW) { Serial.print("[Left]"); } else if (c == PS2_RIGHTARROW) { Serial.print("[Right]"); } else if (c == PS2_UPARROW) { Serial.print("[Up]"); } else if (c == PS2_DOWNARROW) { Serial.print("[Down]"); } else if (c == PS2_DELETE) { Serial.print("[Del]"); } else { // otherwise, just print all normal characters Serial.print(c); } } } PS2 Connector Pinout
Non-US Keyboard LayoutThe read() function returns ASCII codes, which are translated from the keyboard's raw scan codes. This translation is done using two arrays inside PS2Keyboard.cpp, called scan2ascii_noshift and scan2ascii_shift.For non-US keyboard layouts, you may need to edit these arrays for proper output. Special KeysPS2 keyboards have many special keys which do not map to ASCII characters. By default, several are mapped to ASCII control characters with similar intended function. Others are disabled, but may be mapped to any byte by editing PS2Keyboard.h.// Every call to read() returns a single byte for each // keystroke. These configure what byte will be returned // for each "special" key. To ignore a key, use zero. #define PS2_TAB 9 #define PS2_ENTER 13 #define PS2_BACKSPACE 127 #define PS2_ESC 27 #define PS2_INSERT 0 #define PS2_DELETE 127 #define PS2_HOME 0 #define PS2_END 0 #define PS2_PAGEUP 25 #define PS2_PAGEDOWN 26 #define PS2_UPARROW 11 #define PS2_LEFTARROW 8 #define PS2_DOWNARROW 10 #define PS2_RIGHTARROW 21 #define PS2_F1 0 #define PS2_F2 0 #define PS2_F3 0 #define PS2_F4 0 #define PS2_F5 0 #define PS2_F6 0 #define PS2_F7 0 #define PS2_F8 0 #define PS2_F9 0 #define PS2_F10 0 #define PS2_F11 0 #define PS2_F12 0 #define PS2_SCROLL 0 Version 2.4Support Teensy 3.0, Arduino Due, Arduino Leonardo & other boardsFrench keyboard layout, thanks to David Chochoi Version 2.3Minor bugs fixed.Version 2.2Add support for non-US keyboards. German is the only support (yet), thanks to Rainer Bruch who send a German keyboard for testing! See the example in File > Exampels > PS2Keyboard > International.Version 2.1Version 2.1 adds the following improvements:
Version 2.0Version 2.0 adds the following improvements:
DetailsOther useful pages:
|