USB Keyboard as input for Arduino Uno

The very basics


uint8_t servoPos;

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Use + and - to adjust servo angle"));
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    switch (ch)
    {
      case '+':
        if (servoPos <= 170)
        {
          servoPos += 10;
          Serial.println(servoPos);
        }
        break;
      case '-':
        if (servoPos >= 10)
        {
          servoPos -= 10;
          Serial.println(servoPos);
        }
        break;
      default:
        Serial.print(F("Unsupported character 0x"));
        if (ch < 0x10) Serial.print("0");
        Serial.println(ch, HEX);
        break;
    }
  }
}

The above will read one character if a character is available and depending on the character increments or decrements the servoPos or prints a message.

The IDE has a setting to determine if a <CR> (carriage return) and/or <LF> (linefeed) has to be added after the characters that you typed.

I think the default is Both NL * CR which means when you type one character and send it, you will get three characters

Use + and - to adjust servo angle
10
Unsupported character 0x0D
Unsupported character 0x0A

You can change the setting to No line ending.

If you want to send more than a single character, I suggest that you read Serial Input Basics - updated to get ideas how to approach.

That's probably because you are using higher level functions like readBytes, readStringUntil that timeout and you do not check if you got data.