The sketches below are in the examples folder in the library.  The first is one for the Fritzing sketch using two pushbuttons for your own keyboard.

Be sure to select Pro Trinket 5V or Pro Trinket 3V in the Arduino IDE Tools -> Board menu. If you select the original Trinket or another board you will get cryptic compiler errors including "constant not defined".
/*
ProTrinketKeyboard example
For Pro Trinket (ATmega328 based Trinket) by Adafruit Industries
Please use library TrinketKeyboard for the ATtiny85 based Trinket
Version 1.0  2015-01-01 Initial Version derived from TrinketKeyBoardExample  Mike Barela
*/

#include <ProTrinketKeyboard.h>  // Ensure the library is installed

// Switches are connected from ground to these defined pins
const int PIN_BUTTON_CAPITAL_A = 12;
const int PIN_BUTTON_STRING    = 11;

void setup()
{
  // Declare button pins as inputs
  pinMode(PIN_BUTTON_CAPITAL_A, INPUT);
  pinMode(PIN_BUTTON_STRING,    INPUT);

  // setting input pins to high means turning on internal pull-up resistors
  digitalWrite(PIN_BUTTON_CAPITAL_A, HIGH);
  digitalWrite(PIN_BUTTON_STRING,    HIGH);
  // remember, the buttons are active-low, they read LOW when they are not pressed

  // start USB stuff
  TrinketKeyboard.begin();
}

void loop()
{
  TrinketKeyboard.poll();
  // the poll function must be called at least once every 10 ms
  // or cause a keystroke
  // if it is not, then the computer may think that the device
  // has stopped working, and give errors

  if (digitalRead(PIN_BUTTON_CAPITAL_A) == LOW)
  {
    TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_A);
    // this should type a capital A
    TrinketKeyboard.pressKey(0, 0);
    // this releases the key
  }

  if (digitalRead(PIN_BUTTON_STRING) == LOW)
  {
    // type out a string using the Print class
    TrinketKeyboard.print("Hello World! ");
  }
}

Two buttons are placed on a breadboard and connected to Pro Trinket pins 11 and 12. When the first button is pressed, it sends a capital letter A.  If the second button is pressed, the string "Hello World! " is sent.  You can use these function calls to send letters or strings of your choice.

Plug your programmed Pro Trinket into an available USB port.  In Windows, the device will show up in the Control Panel Devices and Printers dialog as "Pro Trinket Keyboard" with a keyboard icon as shown below:

The second example is kind of sneaky.  It assumes you've plugged the Pro Trinket into a PC and it sends random characters to the open operating system window at random times (from 6 to 60 seconds between characters).  Guaranteed to generate a "What the #$%*@!" from the person trying to use their computer.  

/*
ProTrinketKeyboard prank example

For Pro Trinket (ATmega328P based Trinket) by Adafruit Industries
Please use library TrinketKeyboard for the ATtiny85 based Trinket

Version 1.0  2015-01-01 Initial Version by Mike Barela
*/

#include <ProTrinketKeyboard.h>  // include the Adafruit library

void setup()
{
  TrinketKeyboard.begin();  // initialize USB keyboard code
}

void loop()
{
  unsigned long secs_to_wait = random(6, 60); // wait 6 to 60 seconds between keys
  unsigned long time_stamp = millis();
  while (millis() < (time_stamp + (secs_to_wait * 1000))) // wait the random amount of time
  {
    TrinketKeyboard.poll();
    // the poll function must be called at least once every 10 ms
    // or cause a keystroke
    // if it is not, then the computer may think that the device
    // has stopped working, and give errors
  }
  TrinketKeyboard.typeChar((char)random(33, 122)); // type out a random character (valid ASCII)
}

The Microsoft Word screenshot below displays text mostly from the first example (A and Hello World!) but you can see in the circle where the sketch was changed to the random letter sketch.

This guide was first published on Jan 03, 2015. It was last updated on Jan 03, 2015.

This page (Examples) was last updated on Jan 02, 2015.

Text editor powered by tinymce.