USB keyboard emulator problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

USB keyboard emulator problem

by theclassiestofwhales on Fri Jan 12, 2018 10:53 pm

Hi all! As I've posted previously, I'm building an adaptive interface that emulates a space bar for physically disabled computer users. The trinket I'm using runs a modified version of Adafruit's USB keyboard example code, which I've included below.

I've tested it on a variety of devices. With an HP desktop running Windows 10, a Dell laptop running Windows 10, and a Mac mini running the latest version of OS X, it works every single time I plug it in and press the button. However, on my own MacBook Air and my dad's MacBook Pro, every now and then when I plug it in it won't make a space at all, or else it'll take a few pushes of the button to make one happen. All of the devices I tested used USB 3.0 ports, and I used two different cables for testing, one of which I ordered for the project and one of which I use regularly for a USB MIDI keyboard.

Any idea what to do in this situation?

Code is below:

Code: Select all | TOGGLE FULL SIZE
    #include <TrinketKeyboard.h>
    #define PIN_BUTTON_SPACE 0
    boolean depressed = false;
    long count3 = 30;
    long count2 = 1000;
    unsigned long time;
    long time2 = 0;
    boolean antidepressed = true;
    boolean antidepressed2 = true;
    boolean delayed = false;
    void setup()
      // setting input pins to high means turning on internal pull-up resistors
      digitalWrite(PIN_BUTTON_SPACE, HIGH);
      // remember, the buttons are active-low, they read LOW when they are not pressed
      // start USB stuff
      time = millis();
    void loop()
      unsigned long now = millis();
      unsigned long dif = now - time;
      time = now;
      count2 = count2 + dif;
  if (count2 > 1000) {
        if (digitalRead(PIN_BUTTON_SPACE) == LOW) {
            time2 = time2 + 1;
            if (time2 > 200) {         
            if (depressed == false) {
              TrinketKeyboard.pressKey(0, KEYCODE_SPACE);
              TrinketKeyboard.pressKey(0, 0);
              if (delayed == false) {
                delayed = true;             
              depressed = true; 
              antidepressed = false;
              antidepressed2 = false;
              time2 = 0;
              if (antidepressed2 == false) {
              //TrinketKeyboard.pressKey(0, KEYCODE_E);
              TrinketKeyboard.pressKey(0, 0);
              antidepressed2 = true;
        else if (digitalRead(PIN_BUTTON_SPACE) == HIGH) {
          if (antidepressed == false) {
          depressed = false;
          antidepressed = true;             
          TrinketKeyboard.pressKey(0, 0);
    if (delayed == true) {

Posts: 7
Joined: Thu Jan 04, 2018 2:44 pm

Please be positive and constructive with your questions and comments.