Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Last active September 22, 2017 13:00
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 samsheffield/9b712fceb23343016e2d30f878ef352d to your computer and use it in GitHub Desktop.
Save samsheffield/9b712fceb23343016e2d30f878ef352d to your computer and use it in GitHub Desktop.
Basic keyboard example for Adafruit Trinket with simultaneous input
// 3 button keyboard emulator for the 5V Adafruit Trinket.
// Requires the Trinket Keyboard Library from Adafruit (https://github.com/adafruit/Adafruit-Trinket-USB/archive/master.zip)
#include <TrinketKeyboard.h>
// Array to hold keypresses. Trinket can emulate 3 simultaneously pressed keys
uint8_t pressedKeys[3];
void setup() {
// Each pin* uses internal pullup resistors
// *Place a 470 ohm external pullup resistor between Pin 1 and 5V
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
TrinketKeyboard.begin();
}
void loop() {
TrinketKeyboard.poll();
// All keypresses are stored in the pressedKeys array as KEYCODE_* or 0 (which releases the key)
if (digitalRead(0) == LOW)
pressedKeys[0] = KEYCODE_A;
else
pressedKeys[0] = 0;
if (digitalRead(1) == LOW)
pressedKeys[1] = KEYCODE_S;
else
pressedKeys[1] = 0;
if (digitalRead(2) == LOW)
pressedKeys[2] = KEYCODE_D;
else
pressedKeys[2] = 0;
// Update the keypress array and emulate
TrinketKeyboard.pressKey(0, pressedKeys[0], pressedKeys[1], pressedKeys[2]);
// Brief delay to more closely emulate speed of keyboard input
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment