Delayed output using Keyboard.println()

I'm attempting to create a 'button pad' for a game. My program seems to work Ok, but I noticed while testing in Notepad that when I use
Keyboard.println("Button 1 pressed");
It only prints "some" of the output, if I press a few times, it prints more, but never everything. Eventually I noticed if I pressed a bunch, then moved my mouse, it would catch up writing everything else. I doubt this is a problem with the code, but more likely something with windows, or moving the mouse would have no effect.

I tried to search the forum, but couldn't really find anything. Has anyone else seen this and been able to get around it? It seems fine if I take it down to 1 letter, but I'd like to send a couple keystrokes.....

@jpmulhol, welcome

Which board are you using?

Please post your sketch (and please don't forget to use code tags as described in How to get the best out of this forum).

I'm using a Leonardo. I really don't think its an issue with the code, as it seems like the output gets buffered somehow, and released when other HID type actions occur. I have to think its an OS issue.. Edit to add that the Serial output is not delayed, which is why I think it may be OS related.

#include "Keyboard.h"
const int buttonPin[] = {2, 3, 4, 5, 6, 7};
int pinCount = 6;
int buttonState[] = {0, 0, 0, 0, 0, 0};
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};

long lastDebounceTime[] = {0, 0, 0, 0,0 ,0};
long debounceDelay = 50;
void setup() {
  Serial.begin(9600);
  Serial.println("Start Setup");
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    pinMode(buttonPin[thisPin], INPUT_PULLUP);
    digitalWrite(buttonPin[thisPin], HIGH);
  }
  Serial.println("End Setup");
}

// Output actions. Probably the only part that you need to change
int outputAction(int currentButton) {
    Serial.println("CurrentButton = ");
    Serial.println( currentButton );
    if (currentButton == 2) {
      Serial.println("Button 1 ");
      Keyboard.println("Button 1 pressed");
      delay(100);
    }
    else if (currentButton == 3) {
      Serial.println("Button 2 ");
      delay(100);
    }
    else if (currentButton == 4) {
      Serial.println("Button 3 ");
      delay(100);
    }
    else if (currentButton == 5) {
      Serial.println("Button 4 ");
      delay(100);
    }
    else if (currentButton == 6) {
      Serial.println("Button 5 ");
      delay(100);
    }
    else if (currentButton == 7) {
      Serial.println("Button 6 ");
      delay(100);
    }                
}
void loop() {
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    buttonState[thisPin] = digitalRead(buttonPin[thisPin]);

    if ((buttonState[thisPin] != prevButtonState[thisPin]) && (buttonState[thisPin] == HIGH)) {
      if ((millis() - lastDebounceTime[thisPin]) > debounceDelay) {
        outputAction(buttonPin[thisPin]);
        lastDebounceTime[thisPin] = millis();
      }
    }

    prevButtonState[thisPin] = buttonState[thisPin];
  }
}

I got the original code from this article:

I tried individual Keyboard.write() and Keyboard.press/release and they all seem to buffer up. Any input would be appreciated..

I tested the output at CLI on Windows and it works without issue. There must be some input buffering when using Notepad and Word. Hopefully the same issue isn't present in the game I want to play.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.