Library

You can download the code from the link below.  See the tutorial All About Arduino Libraries on installing the code into your Arduino Integrated Development Environment (IDE) for your use.

The functions in the library for interacting with the USB port and sending text are shown below.  You can refer to the examples on the next page to see how they are used.

Download: file
Trinket_Keyboard.begin(); // starts the USB driver, causes re-enumeration

Trinket_Keyboard.isConnected(); // checks if USB is connected, 0 if not connected

// this (or "press" something) must be called at least once every 10ms
Trinket_Keyboard.poll();  // this (or "press" something) must be called at least once every 10ms

// presses up to 6 keys, and modifiers (modifiers are keys like shift, CTRL, etc)
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5, uint8_t keycode6);
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5);
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4);
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3);
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2);
Trinket_Keyboard.pressKey(uint8_t modifiers, uint8_t keycode1);

// presses a list of keys
Trinket_Keyboard.pressKeys(uint8_t modifiers, uint8_t* keycodes, uint8_t sz);

// type out a single ASCII character
Trinket_Keyboard.typeChar(uint8_t ascii);

// returns the state of the three LEDs on a keyboard (caps/num/scroll lock)
Trinket_Keyboard.getLEDstate();

// inherit from "Print", these two write functions are implemented
size_t val = Trinket_Keyboard.write(uint8_t);
using Print::write;
// other "print" and "println" functions are automatically available
using Print::print;
using Print::println;

// helps translate ASCII characters into keycode and modifier combinations, 
// while taking into account whether or not caps lock is on
ASCII_to_keycode(uint8_t ascii, uint8_t ledState, uint8_t* modifier, uint8_t* keycode);

A list of keys and keyboard modifiers is in the file ProTrinketKeyboard.h.

Design your code so you call a keypress or the poll function once every 10 milliseconds.  This allows the connection to the USB host (PC) to "stay alive". Placing the call in the Arduino Loop function is usually sufficient but if you have any code in the body of the loop that waits, include poll calls in those areas also.

This guide was first published on Jan 03, 2015. It was last updated on Jan 03, 2015. This page (Library) was last updated on Aug 21, 2019.