arduino-libraries / Keyboard

GNU Lesser General Public License v3.0
225 stars 158 forks source link

Race Conditions and Garbled Keyboard Output #75

Closed brxken128 closed 1 year ago

brxken128 commented 1 year ago

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();
}
brxken128 commented 1 year ago

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

per1234 commented 1 year ago

Issue is now being tracked at https://github.com/earlephilhower/arduino-pico/issues/928