arduino-libraries/Keyboard

Race Conditions and Garbled Keyboard Output

brxken128 opened this issue · 2 comments

I've noticed an issue that occurs on multiple boards, and all seem to produce the same issue.

While using Keyboard.println(), and while even printing manually, the text will become mangled at some point until the device is unplugged and re-plugged into the PC.

Here's an example sample of output:

hello world hello world hello world
hello world hello world hello world
hello world hello world hello world
hello world hello world hello world
hello world hello world hlelo world
hello world hello wrold hello wrold
hello world hello world hello world
hello world helol world hello world
hlelo world hello world ehllo world

I have tried adding delays with code such as this (to no avail):

for(int i = 0; i < text.length(); i++) {
  Keyboard.write(text[i]);
  delay(2);
}
Keyboard.write(0xB0);

I've also tried setting the delay between keypresses to something absurdly high (50ms), and I've tried calling Keyboard.end() and then Keyboard.begin() before the next entry. Nothing seems to help.

The only way to fix the keyboard is to re-connect the device to the PC, which can get pretty annoying. I assumed it was a race condition at first, but now I'm not sure. I recently bought some more boards, and tested them, and the issue persists. It seems to be even worse now, and re-plugging the device does not restore functionality (nor does re-flashing, which is a little scary).

Here's a sketch - keep pressing the button and eventually the sequence of ab will break:

#include <Keyboard.h>
#include "OneButton.h"

const String text = "abababababababababababababababababababababababababababababababababababababababababababababababababababababababab"

OneButton button(PIN_INPUT, true);

void enterText(String value) {
  Keyboard.println(value);
}

void singleClick() {
  enterText(text);
}

void setup() {
  Keyboard.begin();
  button.attachClick(singleClick);
  delay(1000);
}

void loop() {
  button.tick();
}

Apologies, I should probably open this in the arduino-pico repository.

Issue is now being tracked at earlephilhower/arduino-pico#928