Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
787 changes: 262 additions & 525 deletions PS2Keyboard.cpp

Large diffs are not rendered by default.

20 changes: 7 additions & 13 deletions PS2Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
Written by Christian Weichel <info@32leaves.net>

** Mostly rewritten Paul Stoffregen <paul@pjrc.com>, June 2010
** Modified for use with Arduino 13 by L. Abraham Smith, <n3bah@microcompdesign.com> *
** Modified for easy interrup pin assignement on method begin(datapin,irq_pin). Cuningan <cuninganreset@gmail.com> **
** Modified for use with Arduino 13 by L. Abraham Smith, <n3bah@microcompdesign.com>
** Modified for easy interrupt pin assignment on method begin(datapin,irq_pin). Cuningan <cuninganreset@gmail.com>
** Modified for ESP32/ESP8266 support by Lahiru <lahirunirmalx@gmail.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,12 +26,9 @@

#ifndef PS2Keyboard_h
#define PS2Keyboard_h

#include <Arduino.h> // for attachInterrupt, FALLING

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h" // for attachInterrupt, FALLING
#else
#include "WProgram.h"
#endif

#include "utility/int_pins.h"

Expand Down Expand Up @@ -180,12 +178,8 @@ typedef struct {
} PS2Keymap_t;


// US keyboard layout (default)
extern const PROGMEM PS2Keymap_t PS2Keymap_US;
extern const PROGMEM PS2Keymap_t PS2Keymap_German;
extern const PROGMEM PS2Keymap_t PS2Keymap_French;
extern const PROGMEM PS2Keymap_t PS2Keymap_Spanish;
extern const PROGMEM PS2Keymap_t PS2Keymap_Italian;
extern const PROGMEM PS2Keymap_t PS2Keymap_UK;


/**
Expand Down Expand Up @@ -217,7 +211,7 @@ class PS2Keyboard {
static void clear();

/**
* Retutns ps2 scan code.
* Returns the raw PS/2 scan code.
*/
static uint8_t readScanCode(void);

Expand Down
195 changes: 191 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,194 @@
#PS/2 Keyboard Library#
# PS2Keyboard Library

PS2Keyboard allows you to use a keyboard for user input.
[![License: LGPL v2.1](https://img.shields.io/badge/License-LGPL%20v2.1-blue.svg)](https://www.gnu.org/licenses/lgpl-2.1)
[![Version](https://img.shields.io/badge/version-3.0.0-green.svg)](https://github.com/lahirunirmalx/PS2Keyboard)

http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
A PS/2 keyboard library for Arduino, Teensy, ESP32, and ESP8266 platforms.

![](http://www.pjrc.com/teensy/td_libs_PS2Keyboard.jpg)
## Features

- **Multi-platform support**: Arduino (Uno, Mega, Due, Leonardo), Teensy, ESP32, ESP8266
- **Interrupt-driven**: Efficient interrupt-based input handling
- **Lock key support**: Caps Lock, Num Lock, and Scroll Lock with LED indicators
- **Easy to use**: Simple API with `begin()`, `available()`, and `read()` methods
- **US keyboard layout**: Standard US QWERTY layout included

## Installation

### Arduino IDE

1. Download this repository as a ZIP file
2. In Arduino IDE: `Sketch` → `Include Library` → `Add .ZIP Library...`
3. Select the downloaded ZIP file

### PlatformIO

Add the following to your `platformio.ini`:

```ini
lib_deps =
https://github.com/lahirunirmalx/PS2Keyboard
```

## Wiring

Connect your PS/2 keyboard to your microcontroller:

| PS/2 Pin | Signal | Connect To |
|----------|--------|------------|
| 1 | Data | Data Pin (configurable) |
| 3 | GND | GND |
| 4 | VCC | 5V |
| 5 | Clock | IRQ Pin (configurable) |

> **Note**: PS/2 keyboards require 5V power. Some boards may need level shifters for the data lines.

## Quick Start

```cpp
#include <PS2Keyboard.h>

const int DataPin = 8;
const int IRQpin = 5;

PS2Keyboard keyboard;

void setup() {
Serial.begin(115200);
keyboard.begin(DataPin, IRQpin);
Serial.println("PS/2 Keyboard Ready");
}

void loop() {
if (keyboard.available()) {
char c = keyboard.read();

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_BACKSPACE) {
Serial.print("[Backspace]");
} else {
Serial.print(c);
}
}
}
```

## Valid IRQ Pins

| Board | Valid IRQ Pins |
|-------|----------------|
| Arduino Uno | 2, 3 |
| Arduino Mega | 2, 3, 18, 19, 20, 21 |
| Arduino Due | All pins (except 13) |
| Arduino Leonardo | 0, 1, 2, 3 |
| Teensy 3.x/4.x | All digital pins |
| Teensy 2.0 | 5, 6, 7, 8 |
| ESP32 | All GPIO pins |
| ESP8266 | All GPIO pins |

## API Reference

### Methods

| Method | Description |
|--------|-------------|
| `begin(dataPin, irqPin)` | Initialize the keyboard with data and clock pins |
| `begin(dataPin, irqPin, keymap)` | Initialize with a custom keymap |
| `available()` | Returns `true` if a key is available to read |
| `read()` | Returns the next character (UTF-8 encoded) |
| `readUnicode()` | Returns the next character as Unicode |
| `readScanCode()` | Returns the raw PS/2 scan code |
| `clear()` | Clears the keyboard buffer |

### Special Key Constants

```cpp
PS2_ENTER // Enter key
PS2_TAB // Tab key
PS2_ESC // Escape key
PS2_BACKSPACE // Backspace key
PS2_DELETE // Delete key
PS2_INSERT // Insert key
PS2_HOME // Home key
PS2_END // End key
PS2_PAGEUP // Page Up key
PS2_PAGEDOWN // Page Down key
PS2_UPARROW // Up arrow
PS2_DOWNARROW // Down arrow
PS2_LEFTARROW // Left arrow
PS2_RIGHTARROW // Right arrow
PS2_F1 - PS2_F12 // Function keys
```

## Platform-Specific Configuration

### ESP32

```ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
https://github.com/lahirunirmalx/PS2Keyboard
```

### Arduino Uno

```ini
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200
lib_deps =
https://github.com/lahirunirmalx/PS2Keyboard
```

## Examples

- **Simple_Test**: Basic keyboard input example
- **TypeToDisplay**: Display keyboard input on an LCD

## Version History

- **v3.0.0** (January 2026)
- ESP32 and ESP8266 platform support
- Improved interrupt handling
- Caps Lock, Num Lock, Scroll Lock LED support
- Code cleanup and optimization

- **v2.4** (March 2013)
- Teensy 3.0, Arduino Due, Leonardo support

- **v2.0** (June 2010)
- Buffering, shift key support, indexed lookups

## Credits

- Original library by [PJRC](http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html)
- Christian Weichel - Original author
- Paul Stoffregen - Major rewrite
- L. Abraham Smith - Arduino 13 modifications
- Cuningan - Flexible pin assignment
- Lahiru - ESP32/ESP8266 support and maintenance

## License

This library is licensed under the [GNU Lesser General Public License v2.1](https://www.gnu.org/licenses/lgpl-2.1.html).

## Contributing

Contributions are welcome! Please open an issue or submit a pull request.

## Support

- [GitHub Issues](https://github.com/lahirunirmalx/PS2Keyboard/issues)
- [Arduino Forum](https://forum.arduino.cc)
- [ESP32 Forum](https://www.esp32.com)
34 changes: 0 additions & 34 deletions examples/International/International.ino

This file was deleted.

48 changes: 22 additions & 26 deletions examples/Simple_Test/Simple_Test.ino
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
/* PS2Keyboard library example
/*
PS2Keyboard Library - Simple Test Example

PS2Keyboard now requries both pins specified for begin()

keyboard.begin(data_pin, irq_pin);

Valid irq pins:
Arduino Uno: 2, 3
Arduino Due: All pins, except 13 (LED)
Arduino Mega: 2, 3, 18, 19, 20, 21
Teensy 3.0: All pins, except 13 (LED)
Teensy 2.0: 5, 6, 7, 8
Teensy 1.0: 0, 1, 2, 3, 4, 6, 7, 16
Teensy++ 2.0: 0, 1, 2, 3, 18, 19, 36, 37
Teensy++ 1.0: 0, 1, 2, 3, 18, 19, 36, 37
Sanguino: 2, 10, 11

for more information you can read the original wiki in arduino.cc
at http://www.arduino.cc/playground/Main/PS2Keyboard
or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
This example demonstrates basic PS/2 keyboard input.

Like the Original library and example this is under LGPL license.
Wiring:
PS/2 Data -> DataPin
PS/2 Clock -> IRQpin
PS/2 VCC -> 5V
PS/2 GND -> GND

Valid IRQ pins by board:
Arduino Uno: 2, 3
Arduino Mega: 2, 3, 18, 19, 20, 21
Arduino Due: All pins (except 13)
Teensy 3.x/4.x: All digital pins
ESP32/ESP8266: All GPIO pins

More info: https://github.com/lahirunirmalx/PS2Keyboard

Modified by Cuninganreset@gmail.com on 2010-03-22
Modified by Paul Stoffregen <paul@pjrc.com> June 2010
License: LGPL v2.1
*/

#include <PS2Keyboard.h>

const int DataPin = 8;
const int IRQpin = 5;
const int IRQpin = 5;

PS2Keyboard keyboard;

void setup() {
delay(1000);
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);
Serial.println("Keyboard Test:");
Serial.begin(115200);
Serial.println("PS/2 Keyboard Test - Type to begin:");
}

void loop() {
Expand Down
Loading