PS2Keyboard Library

Hardware Requirements

PS2 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.

Board

Data Pin

Clock Pin (IRQ)

Teensy 3.0

Any, except 13

Any, except 13

Teensy 2.0

Any, except 11

5, 6, 7, 8

Teensy 1.0

Any

0, 1, 2, 3, 4, 6, 7, 16

Teensy++ 2.0

Any, except 6

0, 1, 2, 3, 18, 19, 36, 37

Teensy++ 1.0

Any

0, 1, 2, 3, 18, 19, 36, 37

Basic Usage

PS2Keyboard 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.

The 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.

PS2 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.

Scan Code Buffering: Data captured by the interrupt is stored
into a circular buffer, so several keystrokes can be buffered if your
sketch is busy and can not call read() between each one. Buffering also
retains key break (up) events. Previous versions buffered only a single
scan code and discarded all key break codes.

Shift Key State: The state of both shift keys is tracked, so
conversion to ASCII produces proper upper and lower case characters. The
numbers and other dual character keys also properly respond to shift.
Previous versions were limited to only uppercase and non-shift output.

ASCII Only Output: The read() function returns only ASCII
characters, or special keys mapped to ASCII control characters. Previous
versions returned a mix of ASCII and raw scan codes, with no way to
distinguish between them.