# How to handle user input

**URL:** https://discourse.processing.org/t/how-to-handle-user-input/47163
**Category:** Libraries
**Created:** [September 17, 2025, 3:46pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163 "2025-09-17T15:46:53Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)
#### Post date: [September 17, 2025, 3:46pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/1 "2025-09-17T15:46:53Z")

</div>

So i was wondering what the simplest way to handle 50 some commands would be.

Say a user enters a command in a textfield and the code needs to handle each command separately. Is there something shorter than a if/else or case/break?

Thanks.😎

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [September 17, 2025, 4:29pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/2 "2025-09-17T16:29:33Z")

</div>

Ahhh a minimum information problem 😁

To get a useful answer you might want to include more information such as

- What Processing mode are you using e.g. Java, Python, p5js … ?
- What sort of commands are to being handled e.g. OS system commands … ?
- Some context to the application area.

---

<div class="post-metadata">

### Author: ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)
#### Post date: [September 17, 2025, 6:24pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/3 "2025-09-17T18:24:50Z")

</div>

Thanks for the reply.

> **[GitHub - bonner72/SerialTerminal: A simple serial terminal made with Processing](https://github.com/bonner72/SerialTerminal)**
>
> A simple serial terminal made with Processing

Working on this terminal software(Java mode) and am adding commands for user customization .e.g. (Font type, Color scheme, Language). Mainly to keep UI from being cluttered. The commands would be pretty simple for instance.

```processing
if (input == "f-unifont") {
  systemFont = "unifont";
}

```

So an if statement might not be the worst.

I see now that switch statements won’t work with strings(input from software is string type) so that one is out.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 17, 2025, 8:03pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/4 "2025-09-17T20:03:16Z")

</div>

> [@bonner45](#):
>
> I see now that switch statements won’t work with strings(input from software is string type) so that one is out.

The reference does not show _String_ as an _expression_ but it will work.

_[switch / Reference / Processing.org](https://processing.org/reference/switch.html)_

Example with a String:

```auto
String s = "B1";

//s = "A0";

switch(s) {
  case "A0": 
    println("Alpha"); // Does not execute
    break;
  case "B1": 
    println("Bravo"); // Does not execute
    break;
  default: // Default executes if the case names
    println("None"); // don't match the switch parameter
    break;
}

```

The correct way to compare _Strings_ is discussed here:

> **[String / Reference](https://processing.org/reference/string)**
>
> A string is a sequence of characters. The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for convertin…

`:)`

---

<div class="post-metadata">

### Author: ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)
#### Post date: [September 17, 2025, 10:42pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/5 "2025-09-17T22:42:41Z")

</div>

Thanks @glv.

It seems that a switch or if statement are the best options.

Thanks for the help.

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [September 19, 2025, 6:34am UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/6 "2025-09-19T06:34:40Z")

</div>

> [@bonner45](#):
>
> Is there something shorter than a if/else or case/break?

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

```plaintext
|cmd|function|

```

e.g.

```plaintext
|abc|function 1|
|xyz|function 2|

```

For this you can use a HashMap ([HashMap / Reference / Processing.org](https://processing.org/reference/HashMap.html)).

First you need to create the java equivalent of a function pointer (see e.g. [How to use Function Pointers in Java | Gregory Gaines](https://www.gregorygaines.com/blog/how-to-use-function-pointers-in-java/)). Note that this is outside my area of knowledge so don’t ask.

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

```

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

```java
// 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

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

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

```

Now you can populate the lookup table

```java
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

```java
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.

> [@bonner45](#):
>
> The commands would be pretty simple for instance.
> 
> ```auto
> if (input == "f-unifont") {
> systemFont = "unifont";
> }
> 
> ```

You can write a function _setUnifont_

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

```

and you can add that entry in _setup()_ using

```java
lookupTable.put("f-unifont", this::setUnifont);

```

---

<div class="post-metadata">

### Author: ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)
#### Post date: [September 19, 2025, 10:10am UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/7 "2025-09-19T10:10:28Z")

</div>

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.

```java
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.

```java
  // 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_).

---

<div class="post-metadata">

### Author: ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)
#### Post date: [September 19, 2025, 6:38pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/8 "2025-09-19T18:38:30Z")

</div>

Thank you @sterretje i will look into it.

---

<div class="post-metadata">

### Author: ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)
#### Post date: [September 22, 2025, 9:07pm UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/9 "2025-09-22T21:07:43Z")

</div>

Interesting. In version 3 strings were not supported in switch. I have used a work-around, functions as a switch but also allows the use of variables in the case equivalent.

```auto
void setup(){
  String[] x= {"A","B","Z",str(TAB),"V"};
  String somethingVariable= "Q";
  
  for( String a:x){
  print(a+": ");  
  commandLoop:
    {
      if( a.equals("A") ){
        // handle command A
        println("command A");
        break commandLoop;
      }
      
      if( a.equals("B") ){
        // handle command B
        println("command B");
        break commandLoop;
      }
      
      if( a.equals("Z") ){
        // handle command Z
        println("command Z");
        break commandLoop;
      }

      if( a.equals(str(TAB)) ){
        // handle command TAB
        println("command TAB");
        break commandLoop;
      }
      
      if( a.equals(somethingVariable) ){
        // handle command somethingVariable
        println("command somethingVariable",somethingVariable);
        break commandLoop;
      }
      
      // default
      println("command",a,"unknown");
      
    } // end commandLoop

}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 23, 2025, 12:33am UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/10 "2025-09-23T00:33:46Z")

</div>

> [@rhole](#):
>
> In version 3 strings were not supported in switch.

Java 7 and high support strings in switch statements.  
I can verify that Processing 3.5.4 works with your example.

Open Gemini response:

**A Brief History**

- **Early Versions:** Processing was started in 2001, so it initially used older versions of Java (like Java 1.3 or 1.4).
- **Processing 2.x:** The major release of **Processing 2** (released between 2012 and 2014) was built on top of **Java 6** , but it could also run on Java 7. The developers began to incorporate features from newer Java releases as they became stable.
- **Processing 3.x:** Processing 3, released in 2015, made the transition to **Java 8** , which was a significant update with new language features like lambdas.
- **Current Versions:** The latest versions of Processing are now built on **Java 17**.

`:)`

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [September 23, 2025, 1:34am UTC](https://discourse.processing.org/t/how-to-handle-user-input/47163/11 "2025-09-23T01:34:39Z")

</div>

> [@glv](#):
>
> **Processing 3.x:** Processing 3, released in 2015, made the transition to **Java 8** , which was a significant update with new language features like lambdas.

Even though it used Java 8, we couldn’t use lambda syntax at all!
