Arduino - KeyboardController

Examples > USBHost Library

Keyboard Controller

The Arduino Due has the ability to act as a USB host for peripherals such as a keyboard connected to the SerialUSB port. This example demonstrates the use of the KeyboardController library.

Hardware Required

  • Arduino Due Board

  • USB keyboard (NB : keyboards that connect through an internal USB hub, like Apple keyboards, will not work)



 Keyboard Controller HID Example

 Shows the output of a USB Keyboard connected to the USB

 controller of an Arduino Due Board.

 created 8 Oct 2012

 by Cristian Maglie


// Require keyboard control library
#include <KeyboardController.h>

// Initialize USB Controller

USBHost usb;

// Attach keyboard controller to USB

KeyboardController keyboard(usb);

// This function intercepts key press
void keyPressed() {

  Serial.print("Pressed:  ");


// This function intercepts key release
void keyReleased() {

  Serial.print("Released: ");


void printKey() {

  // getOemKey() returns the OEM-code associated with the key

  Serial.print(" key:");


  // getModifiers() returns a bits field with the modifiers-keys

  int mod = keyboard.getModifiers();

  Serial.print(" mod:");


  Serial.print(" => ");

  if (mod & LeftCtrl)

    Serial.print("L-Ctrl ");

  if (mod & LeftShift)

    Serial.print("L-Shift ");

  if (mod & Alt)

    Serial.print("Alt ");

  if (mod & LeftCmd)

    Serial.print("L-Cmd ");

  if (mod & RightCtrl)

    Serial.print("R-Ctrl ");

  if (mod & RightShift)

    Serial.print("R-Shift ");

  if (mod & AltGr)

    Serial.print("AltGr ");

  if (mod & RightCmd)

    Serial.print("R-Cmd ");

  // getKey() returns the ASCII translation of OEM key

  // combined with modifiers.



void setup()


  Serial.println("Program started");


void loop()

  // Process USB tasks


See also