Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Keyboard Controller Example
Shows the output of a USB Keyboard connected to
the Native USB port on an Arduino Due Board.
created 8 Oct 2012
by Cristian Maglie
This sample code is part of the public domain.
// Require keyboard control library
#include <KeyboardController.h>
// Initialize USB Controller
USBHost usb;
// Attach keyboard controller to USB
KeyboardController keyboard(usb);
void printKey();
// This function intercepts key press
void keyPressed() {
Serial1.print("Pressed: ");
// This function intercepts key release
void keyReleased() {
Serial1.print("Released: ");
void printKey() {
// getOemKey() returns the OEM-code associated with the key
Serial1.print(" key:");
// getModifiers() returns a bits field with the modifiers-keys
int mod = keyboard.getModifiers();
Serial1.print(" mod:");
Serial1.print(" => ");
if (mod & LeftCtrl)
Serial1.print("L-Ctrl ");
if (mod & LeftShift)
Serial1.print("L-Shift ");
if (mod & Alt)
Serial1.print("Alt ");
if (mod & LeftCmd)
Serial1.print("L-Cmd ");
if (mod & RightCtrl)
Serial1.print("R-Ctrl ");
if (mod & RightShift)
Serial1.print("R-Shift ");
if (mod & AltGr)
Serial1.print("AltGr ");
if (mod & RightCmd)
Serial1.print("R-Cmd ");
// getKey() returns the ASCII translation of OEM key
// combined with modifiers.
void setup()
Serial1.begin( 57600 );
while (!Serial1); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
Serial1.println("Keyboard Controller Program started");
if (usb.Init() == -1)
Serial1.println("OSC did not start.");
delay( 20 );
digitalWrite(LED_BUILTIN, HIGH);
Serial1.println("...still alive");
void loop()
// Process USB tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.