How to handle user input

Here is an alternative approach for the keyPressed() function. There was a reason why I used a HashMap (and not an array) but next forgot; one can easily retrieve the value based on the key and one can easily check if the entered command exists.

void keyPressed()
{
  // collect user input; for demo only 'a'..'z'
  if (key >= 'a' && key <='z')
  {
    userInput += key;
  } else
  {
    // linefeed terminates user input
    if (key == '\n')
    {
      // show user input
      println("'" + userInput + "'");

      if(lookupTable.containsKey(userInput))
      {
        if(lookupTable.get(userInput) != null)
        {
          lookupTable.get(userInput).execute();
        }
        else
        {
          println("Function not specified for command '" + userInput + "'");
        }
      }
      else
      {
        println("unknown command '" + userInput + "'");
      }

      // clear the user input
      userInput = "";
    }
  }
}

It also contains hardening in case you have not implemented a function for a command yet but the entry in the lookup table was prepared.

  // link user input and functions
  lookupTable.put("abc", this::func1);
  lookupTable.put("xyz", this::func2);
  lookupTable.put("x", null);

Command ‘x’ was prepared but no function was assigned yet (hence null).