How to handle user input

Yes, there is.

I’m not much of a Processing/java programmer, I hang around more in the C/C++ world where function pointers are common and very suitable for this task; unfortunately Processing/java does not directly have function pointers.

The idea is to have a lookup table that looks like

|cmd|function|

e.g.

|abc|function 1|
|xyz|function 2|

For this you can use a HashMap (HashMap / Reference / Processing.org).

First you need to create the java equivalent of a function pointer (see e.g. How to use Function Pointers in Java | Gregory Gaines). Note that this is outside my area of knowledge so don’t ask.

// Wrapping interface
private interface FunctionPointer
{
  // Method signatures of pointed method
  void execute();
}

Now you can create a HashMap; I called it lookupTable.

// this hashmap links a string to a function
HashMap<String, FunctionPointer> lookupTable = new HashMap<String, FunctionPointer>();

Before you can populate the lookup table you need to define your functions; below two simple functions

public void func1()
{
  println("Called func1");
}

public void func2()
{
  println("Called func2");
}

Now you can populate the lookup table

void setup()
{
  // link user input and functions
  lookupTable.put("abc", this::func1);
  lookupTable.put("xyz", this::func2);
}

When user input is received, you can compare it with the keys of the entries in the lookup table; if it matches you can execute the associated function.

Full demo code below

import java.util.Map;

String userInput = "";


/********************************************
 Function pointer related
 Source: https://www.gregorygaines.com/blog/how-to-use-function-pointers-in-java/
 ********************************************/
// Wrapping interface
private interface FunctionPointer
{
  // Method signatures of pointed method
  void execute();
}

/********************************************
 HashMap
 Source: https://processing.org/reference/HashMap.html
 ********************************************/
// this hashmap links a string to a function
HashMap<String, FunctionPointer> lookupTable = new HashMap<String, FunctionPointer>();

/********************************************
 Your functions
 ********************************************/
public void func1()
{
  println("Called func1");
}

public void func2()
{
  println("Called func2");
}


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

  // basic demo
  //FunctionPointer pointer1 = this::func1;
  //FunctionPointer pointer2 = this::func2;
  //pointer1.execute();
  //pointer2.execute();
}

void draw()
{
}

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 + "'");
      // loop through hashmap
      for (Map.Entry e : lookupTable.entrySet())
      {
        // if user input matches key
        if (userInput.equals(e.getKey()))
        {
          // get function pointer
          FunctionPointer fp = (FunctionPointer)e.getValue();
          // and execute
          fp.execute();
        }
      }

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

When you type abc and press <Enter> function func1 is executed.
When you type xyz and press <Enter> function func2 is executed.

You can write a function setUnifont

void setUnifont()
{
  systemFont = "unifont";
}

and you can add that entry in setup() using

lookupTable.put("f-unifont", this::setUnifont);
2 Likes