Itsybitsy RP2040: Arduino (USB) Keyboard library Modifiers not registered properly

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mcody
 
Posts: 10
Joined: Sat Mar 26, 2022 7:03 pm

Itsybitsy RP2040: Arduino (USB) Keyboard library Modifiers not registered properly

Post by mcody »

Apologies if this should be in the Arduino section of the Forum. Being specific to two Itsybitsy products, I decided to post it here. I didn't see an option for cross-posting, so I may post this again in the Arduino Forum.
A few months ago, I made a macro keyboard using an Itsybitsy 32u4 5V. It uses the Arduino (USB) Keyboard library and works well. Now, I am upgrading the capabilities of the macro keyboard and decided to use an Itsybitsy RP2040 to provide more processing power. What I've discovered, though, is a problem with the Arduino (USB) Keyboard library. Specifically, the keyboard modifiers do not appear to register properly. Here are a couple of example code snippets.
This first snippet is the Linux Unicode entry sequence:

Code: Select all

Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('u');
Keyboard.write(unicodeChars[0]);
Keyboard.write(unicodeChars[1]);
Keyboard.write(unicodeChars[2]);
Keyboard.write(unicodeChars[3]);
Keyboard.releaseAll();
This second snippet is the Microsoft Windows Unicode entry sequence:

Code: Select all

Keyboard.write(unicodeChars[0]);
Keyboard.write(unicodeChars[1]);
Keyboard.write(unicodeChars[2]);
Keyboard.write(unicodeChars[3]);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('x');
Keyboard.releaseAll();
Both of these code snippets work properly with the Itsybitsy 32u4 5V, but don't work at all with the Itsybitsy 32u4. If I remove the Keyboard.press lines, the other characters are received and displayed by the computer. They are not registered as Unicode, of course, because of the absence of the pressed Shift and Ctrl keys, in the first snippet, and the pressed Alt key, in the second snippet.
I've been looking through the Arduino (USB) Keyboard sources and the (USB) Keyboard support code in the Philhower RP2040 Arduino support GitHub, but I have yet to find anything pointing to a solution. Any help would be appreciated!

User avatar
mcody
 
Posts: 10
Joined: Sat Mar 26, 2022 7:03 pm

Re: Itsybitsy RP2040: Arduino (USB) Keyboard library Modifiers not registered properly

Post by mcody »

I found out what the problem was. The RP2040 needs to have some amount of delay after each key is transmitted. The RP2040 runs a lot faster than the Atmega32u4! I discovered this need when I found this project: Pico-Matrix-Touch-Keyboard (https://github.com/DustinWatts/Pico-Mat ... h-Keyboard). The revealing bit of code is found in the sketch Pico-Matrix-Touch-Keyboard.ino:

Code: Select all

Keyboard.press(KEY_LEFT_CTRL);
delay(keydelay);
Keyboard.press(KEY_LEFT_ALT);
delay(keydelay);
Keyboard.press(KEY_F1);
delay(keydelay);
Keyboard.releaseAll();
The value of keydelay is set to 100 (milliseconds). Through some experimentation, I found that this delay can be shorter.
For my application, the following code appears to work properly for Linux Unicode injection:

Code: Select all

Keyboard.press(KEY_LEFT_SHIFT);
delay(20);
Keyboard.press(KEY_LEFT_CTRL);
delay(20);
Keyboard.write('u');
Keyboard.write(unicodeChars[0]);
Keyboard.write(unicodeChars[1]);
Keyboard.write(unicodeChars[2]);
Keyboard.write(unicodeChars[3]);
Keyboard.releaseAll();
delay(20);
The following code works for Microsoft Windows Unicode injection:

Code: Select all

Keyboard.write(unicodeChars[0]);
Keyboard.write(unicodeChars[1]);
Keyboard.write(unicodeChars[2]);
Keyboard.write(unicodeChars[3]);
Keyboard.press(KEY_LEFT_ALT);
delay(20);
Keyboard.press('x');
delay(20);
Keyboard.releaseAll();
delay(20);
It may be possible that the delay is most needed for a Keyboard.press without Keyboard.release immediately following. This is what occurs with Keyboard.write. I added a delay after Keyboard.releaseAll just to be safe.

While I'm happy to find the solution, I feel that my Google-fu failed me before I posted my request for help. Well, I hope that this helps someone else in the future!

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”