Printing newlines using Keyboard.h is inconsistent

Hello everyone,
I'm currently in the process of using an Arduino Pro Micro clone to make a keyboard device. While most of the features work as expected, I've been getting strange results with using the Keyboard library to print newlines.

As an example, if my code calls the "TypeStringWithNewline" function below with the "helloworld" string, this is what happens:

1st call : helloworld [CRLF]
2nd call: helloworld (no newline)
3rd call: hellworld [CRLF]
4th + 5th call: helloworld (no newline)
6th call: helloworld [CRLF]

However, if I call TypeStringWithNewline with different inputs, it prints the newline each time.
Has anyone come across this issue before? I'm running this on a Windows 11, with a USB-C-USB3.0 connector between the Arduino and the PC.

Much appreciated in advance!

commandHandlers.cpp file below:

#include "commandHandlers.h"

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

namespace CommandHandlers
{    
    void HandleCommand(const Command& command)
    {
        switch(command.command)
        {
        case CommandType::TYPESTRING:
            TypeString(command.args);
            break;
        case CommandType::TYPESTRINGNEWLINE:
            TypeStringWithNewline(command.args);
            break;
        case CommandType::ALTF4:
            AltF4();
            break;
        case CommandType::ALTTAB:
            AltTab(atoi(command.args));
            break;
        case CommandType::NONE:
            break;
        default:
            break;
        }
        return;
    }    

    void AltTab(int count)
    {
        Keyboard.press(KEY_LEFT_ALT);
        for(int i = 0; i < count; ++i)
        {
            Keyboard.write(KEY_TAB);
        }
        Keyboard.release(KEY_LEFT_ALT);
    }

    void AltF4()
    {
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press(KEY_F4);
        Keyboard.release(KEY_F4);
        Keyboard.release(KEY_LEFT_ALT);
    }

    void TypeString(const char* message)
    {
        Keyboard.print(message);
    }
    void TypeStringWithNewline(const char* message)
    {
        Keyboard.println(message);
    }
}

Showing us a complete sketch that illustrates the problem you are describing rather than an incomplete snippet that no one can compile and test for themselves is much more likely to result in you receiving the help for which you are looking.

Here's the minimum reproducible sketch

#include <Keyboard.h>

void setup() {
    Keyboard.begin();
}

void loop() {
    Keyboard.println("helloworld");
    delay(1000);
}

Excellent.

Unfortunately, the sketch worked exactly as I'd expected it to, producing the following results:

helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld

No inconsistencies, no mysterious missing newlines, only repeated, as expected, output. Or, input, depending on how you look at it.

Great to hear that the sketch is working as expected on your machine.
Turns out it was an issue with using Notepad++ as the text editor - when entering repeated texts, the newline at the end of the Keyboard.println was used to confirm the autocomplete prompt instead of inserting an actual newline at the end of the string.

Sorry that I'm laughing :smiley:

It could have happened to me as well :grimacing:

1 Like

Great that it made someone laugh at least haha
I was looking at this for a good solid two weeks trying to figure out what the hell was going on. Managed to get some unit tests and refactors along the way though, so not an absolute loss!

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