Skip to content

Instantly share code, notes, and snippets.

@NT7S
Created March 5, 2017 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NT7S/534dbf07cf266d1b3978dcef1a945ee7 to your computer and use it in GitHub Desktop.
Save NT7S/534dbf07cf266d1b3978dcef1a945ee7 to your computer and use it in GitHub Desktop.
/*
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
http://arduino.cc/en/Tutorial/KeyboardController
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: ");
printKey();
}
// This function intercepts key release
void keyReleased() {
Serial1.print("Released: ");
printKey();
}
void printKey() {
// getOemKey() returns the OEM-code associated with the key
Serial1.print(" key:");
Serial1.print(keyboard.getOemKey());
// getModifiers() returns a bits field with the modifiers-keys
int mod = keyboard.getModifiers();
Serial1.print(" mod:");
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.
Serial1.write(keyboard.getKey());
Serial1.println();
}
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 );
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial1.println("...still alive");
}
void loop()
{
// Process USB tasks
usb.Task();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment