Skip to content

Keyboard API

NicoHood edited this page Mar 13, 2021 · 16 revisions

Keyboard Layouts

When working on a keyboard project, you first need to decide whether you want to work on a character or on a key basis.

For instance, if you want to make a big button that simulates the press of the Y key, you're better off working at the character level. This is because keyboards are not the same all over the world and while it might be natural for you to think of the Y key as sitting between your T and U keys, you might be surprised at knowing that this might not be the case for German people, as they have a Z in there. This means that the actual key corresponding to the character to want to type depends upon the particular keyboard layout in use. Recent releases of the HID Library have support for the most common layouts, so that they will always press the key corresponding to the character you want to type. Just use:

Keyboard.write('Y');

For this to work, you will need to add the following at the top of your sketch:

#define HID_CUSTOM_LAYOUT
#define LAYOUT_GERMAN

Make sure to add it before the #include <HID-Project.h> directive. Of course you can customize the second line to choose your desired keylayout. You can have a glimpse at the available layouts at the bottom of the ImprovedKeylayouts.h file.

Keep in mind that not all characters can be typed with all the keylayouts. This is because the API does not support AltGr combined ASCII lookup tables nor characters requiring a sequence of keypresses to be entered (e.g.: accents on dead keys).

On the other hand, sometimes you want to press a particular key, regardless of what character it will print. This might be the case when working on a full keyboard, for instance: when the user presses the key between T and U, you will want to send the scancode assigned to that particular key by the USB keyboard specs, it will be the job of the host device to decide what character it corresponds to, according to the keymap it is configured for. In this case you should always refer to the US keylayout using the key constants defined here. Make sure you use the name, not the number: Keyboard.write(0xB0); will not work, use Keyboard.write(KEY_RETURN); instead.

Note that European keyboards have one more key, "cut out" from the left Shift key, that corresponds to KEY_NON_US. They also have a big Enter key, spanning two rows. The \ key has thus been moved to the lower row, next to KEY_QUOTE, but it still corresponds to KEY_BACKSLASH, even though it might respond to KEY_NON_US_NUM on some keyboards.

US Keyboard Layout CC BY-SA 3.0

Improved Keyboard

http://arduino.cc/en/Reference/MouseKeyboard

The improved keyboard has all of the features the normal Arduino keyboard also has. This means your sketches are almost 100% compatible. Its adds the following features:

  • Improved API
  • Added raw keycode support
  • More keys are available (like volume control)
  • Added consumer keys (linux)

The original key definitions still work, just make sure you use the name, not the number. Keyboard.write(0xB0); will not work, use Keyboard.write(KEY_RETURN); instead.

The Improved Keyboard API is available as single (BootKeyboard) and multi report (Keyboard). BootKeyboard adds some more functions such as Led and BIOS support.

Some newer UEFI PCs might support this keyboard as well. BIOS PCs and some other UEFI PCs dont. Please refer to the BootKeyboard then.

All other languages than US are NOT supported. Other layouts are not possible because the API does not support ALT+GR combined ascii lookup tables. Please use the TeensyKeyboard API if you need to use a different layout.

Supported Layouts:

  • US_ENGLISH
  • LAYOUT_GERMAN
  • LAYOUT_FRENCH
  • LAYOUT_ITALIAN
  • LAYOUT_SPANISH
  • A lot more

Boot Keyboard

Inherits all features from the improved keyboard. This uses a single report. You do not essentially need to use this only for BIOS compatibility, this is just and addition to the keyboard. The main differences are:

  • Uses a single report
  • Works with BIOS (and normal PC mode)
  • Could work with smartphones (feedback appreciated)
  • 8 Leds (Shift, Caps Lock, etc.)
  • Supports feature reports

NKRO Keyboard

Consists of 8 Modifiers + 104 fixed Ascii keys + 1 single custom 1 byte key = 113 key can be pressed at the same time.

NKRO Keyboard also supports the Led functions.