PC Services

 

PC Services (Electronics)

Arduino - Hardware Libraries

Tel: 0118 946 3634
Email

linked-in profile facebook profile

   The Company 
   Resources / Examples 
     Libraries 
     Examples 
   Commentaries           
   Personal 

PS/2 keyboard

This collection of libraries are for handling all aspects of the PS/2 Keyboard protocol and each library has examples. Each library has different levels of capabilities depending on your requirements from basic keycode for all keys, through handling keyboard functionality to converting keycodes to UTF-8 (single byte) codes for localisation support.

By all keycodes, the libraries can cope with PS/2 keycodes from any layout, even ones with multi-media, internet or even 24 function keys (yes they do exist). There are many variants and all keycodes includes the special keys for localisations (different countries).

PS/2 Interface

There are many resources detailing the cabling, connectors, voltage levels and protocol for PS/2 Keyboards, so that information is not going to be repeated here, but links to the main online resources, as follows -

Special Hardware Note

PS/2 keyboards are 5V, and even though they use open collector (or open drain) outputs on the keyboard, most have a pull-up resistor to 5V inside the keyboard, so will produce 5V signals.

When using 3V3 boards like the Due and similar, you must put in some form of bidirectional level translator, to protect your board. Many exist, but FET based or IC based ones perform the best.

We found the Texas Instruments 2 port translator TXS0102DCU, to perform well, they are also available in 4 and 8 port versions if you need to convert other 5V signals as well.

Background On Requirements Collection

These libraries have been tested on Uno, Mega and Due boards using Arduino IDE 1.6.7 initially.

The first phase of this library set creation was a lot of research, gathering all known PS/2 keycodes and control codes, with particular tests on how some keys function on actual keyboards. Did you know the <Pause> key has an four byte code sequence on FIRST press but if held down (for auto-repeat) or used with <Alt> key on many keyboards then only gives the last TWO bytes?

When handling internationalisation and other keyboard layouts, we have all seen the multi-media or internet special keys, but did you know you can have -

  • 24 Function keys
  • Some numeric keypads with
    • extra '=' key
    • extra comma key
  • Special keys for switching key layout
  • Special keys for multi key sequences (especially for multi keys in some languages)
  • Some keyboards have FOUR LEDs on them
  • What the handling of NUM LOCK and SCROLL LOCK at same time does

Having collated data together, and some data on other country layouts. We reviewed other libraries out there and find that most suffered from

  • Excessive size for tables (too much RAM or FLASH space used)
  • Lack of choosing or not choosing which countries to support
  • Difficult to add more country keyboard layout support
  • Inability to fully follow the PS/2 protocol
  • Poor coverage of decoding keys
  • Most could not set the keyboard LEDs or change key repeat rates

So a set of libraries and examples were built up (and tested), to cover varying levels of need and provide simple testing abilities. All libraries have an examples, starting with output to serial to where appropriate output to attached LCD display.

The Library set

The libraries are split into three separate libraries, (one is an extension of another).

  • PS2KeyRaw - gives just the keycodes as they are pressed for Make and Break codes as well as autorepeat
  • PS2KeyAdvanced - fully supports PS/2 keyboard protocol
    • Gives key codes along with modifier keys and Make/Break status in one 16 bit word
    • Handles Lock keys and lighting of LEDs
    • Has functions to use other commands in protocol like change repeat rate
    • Can be configured to ignore repeated Make keycode on keys such as SHIFT, CTRL, ALT, etc..
    • Provides A-Z keys as Upper case ASCII assuming US/GB keyboard
    • Number keys are ASCII for 0-9
    • All keys have a code
    • Main Header file has constants for all codes e.g.
      • PS2_KEY_NUM
      • PS2_KEY_L_ARROW
      • PS2_KEY_KP1
      • PS2_KEY_A
      • PS2_KEY_F11
      • PS2_KEY_VOL_UP
      • PS2_KEY_WEB_SEARCH
      • PS2_KEY_POWER
      • PS2_KEY_LANG2
  • PS2KeyMap - Adds translations of keys from PS2KeyAdvanced to ASCII/UTF-8 (single byte) codes in a 16 bit word, default countries supported US and GB and allows additional countries to be added to mapping. Follows NUM, CAPS, SCROLL Lock and Shift for key codes.

    (Requires PS2KeyAdvanced library installed as well)

Example of Serial output for each library using the following Key press sequence only pressing ONCE

  • PAUSE
  • NUM LOCK
  • Numeric Keypad 4
  • S
PS2KeyRaw - SimpleTest.ino
PS2 Raw Test of PS2 Keyboard codes E1 14 77 E1 F0 14 F0 77 77 F0 77 6B F0 6B 1B F0 1B   The first line is MAKE code only for PAUSE key
the first line of each following pair is
the MAKE code for each key
followed by the BREAK code.
PS2KeyAdvanced - advcodetest.ino
PS2 Advanced Key - Advanced Test: 6 EE 101 FA FA 24 8024 53 Get Scancode set in use FA FA 2 8053   6 = Pause key
EE = Keyboard Echo command
101 = NUM Lock key
FA, FA = responses to sending turn NUM lock LED ON
24, 8024 = Make then Break for 4 key
53 = Make Code S key
    (on this example this sends a command to the keyboard)
FA, FA, 2 = response codes to GetScanCode from Keyboard in this case 2
8053 = Break Code S key
PS2KeyMap - International.ino
PS2KeyMap plus PS2KeyAdvanced Libraries International Keyboard Test: Default is US layout, type a key to change layout U for US G for GB/UK D for DE F for FR All keys on keyboard echoed here Value 6 mapped 6 - Status Bits 0 Code 6 ( ) Value 101 Keyboard protocol or function Value FA mapped FA - Status Bits 0 Code FA ( ú ) Value FA mapped FA - Status Bits 0 Code FA ( ú ) Value 24 mapped 34 - Status Bits 0 Code 34 ( 4 ) Value 53 mapped 73 - Status Bits 0 Code 73 ( s )   First lines are to show how to change key mapping
As Before but break codes disabled
6 = Pause key
101 = NUM Lock key
FA, FA = responses to sending turn NUM lock LED ON
24 = Numeric Keypad 4 key mapped in NUM Lock mode to ASCII for '4'
53 = S key as not shifted or in CAPS lock mapped to lower case 's'

Obtaining the libraries

The libraries and documentation are available from the Library Manager in the Arduino IDE V1.6.x and above. By following method -

  • Select Sketch menu
  • Select line Include Library
  • From sub-menu select Manage Libraries...
  • Wait for list to populate then enter into Filter your search box PS2Key
  • Select from list in turn the libraries you want from -
    • PS2KeyRaw
    • PS2KeyAdvanced
    • PS2KeyMap (note this requires PS2KeyAdvanced as well)
  • Select Install and wait for install to complete
Library manager filtered search

Code and Documentation

The source repository for the files if updated and not available from Arduino IDE is Github repository.

Interested in something similar contact sales.

© 2016 onwards by PC Services, Reading UK Last Updated: 17th May 2018
If you encounter problems with this page please email your comments to webmaster