# Simple text input program

**URL:** <https://discourse.processing.org/t/simple-text-input-program/26005>\
**Category:** Gallery\
**Created:** [December 6, 2020, 9:43pm UTC](https://discourse.processing.org/t/simple-text-input-program/26005 "2020-12-06T21:43:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 6, 2020, 9:43pm UTC](https://discourse.processing.org/t/simple-text-input-program/26005/1 "2020-12-06T21:43:26Z")

</div>

A program that allows to type in text. Whenever you hit enter, it will print it into the console.

Features:

- Can enter letters from the “acceptableInput” string
- use arrow keys (LEFT and RIGHT) or click on the text to edit text
- use BACKSPACE / DELETE to remove letters
- ENTER to erase input and print it into the console
- flexible interface
- blinker

```auto
String input = "";
int buffer = 20, minWidth = 200, cursorLocation = 0, lastInput = 0, resumeBlinkAfter = 300;
String acceptableInput = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789.,-+ !?<>()'$%&/[]#{}~=:;" + char('"');

void setup() {
  size(200,100);
  textAlign(3,3);
  surface.setResizable(true);
}

void draw() {
  background(0);
  fill(255);
  text(input,width/2,height/2);
  textSize(20);
  text("Input:",width/2,height/4);
  stroke( ((second()%2 == 0 || lastInput + resumeBlinkAfter > millis())? color(255,0,0,255) : color(255,0,0,0)));
  line(width/2-textWidth(input)/2+textWidth(getShorterString(input,cursorLocation)), height/2 - 10, width/2-textWidth(input)/2+textWidth(getShorterString(input,cursorLocation)), height/2+10);
}

void keyPressed() {
  if(key == ENTER) {
    cursorLocation = 0;
    println(input);
    input = "";
  } else if (key == BACKSPACE) {
    String prefix = getShorterString(input,cursorLocation-1);
    String suffix = getAfterText(input,cursorLocation);
    input = prefix+suffix;
    cursorLocation= ((cursorLocation < 0)? 0 : cursorLocation-1);
  } else if(key == DELETE) {
    String prefix = getShorterString(input,cursorLocation);
    String suffix = getAfterText(input,cursorLocation+1);
    input = prefix+suffix;
  } else {
    for(int i = 0; i < acceptableInput.length(); i++) {
      if(key == acceptableInput.charAt(i)) {
        String prefix = getShorterString(input,cursorLocation), suffix = getAfterText(input,cursorLocation);
        input += key;
        input = (prefix + key + suffix);
        cursorLocation+=1;
        lastInput = millis();
      }
    }
  }
  if(keyCode == LEFT) {
    cursorLocation = ((cursorLocation < 1)? 0 : cursorLocation - 1);
  } else if(keyCode == RIGHT) {
    cursorLocation = ((cursorLocation > input.length()-1)? input.length() : cursorLocation + 1);
  }
  resizeWindow();
  if(input.length() > 4) {
  }
  
}

void mousePressed() {
  float beginTextX = width/2 - textWidth(input)/2;
  for(int i = 0; i < input.length()+1; i++) {
    if(mouseX > textWidth(getShorterString(input,i-1)) + beginTextX && mouseX < textWidth(getShorterString(input,i)) + beginTextX) {
      cursorLocation = i-1;
    }
  }
}

String getShorterString(String text, int len) {
  String newText = "";
  if(len < 0) {
    return("");
  }
  for(int i = 0; i < len && i <= text.length(); i++) {
    newText += text.charAt(i);
  }
  return(newText);
}

String getAfterText(String text, int begin) {
  if(begin < 0 || begin > text.length()) {
    return("");
  }
  String newText = "";
  for(int i = begin; i < text.length(); i++) {
    newText+= text.charAt(i);
  }
  return(newText);
}

void resizeWindow() {
  float setWidth = textWidth(input) + 2*buffer;
  setWidth = ((setWidth > minWidth)? setWidth : minWidth);
  surface.setSize(floor(setWidth),height);
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 6, 2020, 9:46pm UTC](https://discourse.processing.org/t/simple-text-input-program/26005/2 "2020-12-06T21:46:45Z")

</div>

this is an image, showing the program

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b191724a9350568fe7d4295e6656c870c8b0046d.png)

---

<div class="post-metadata">

**Author:** ![uGuigi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/uguigi/32/17360_2.png) [@uGuigi](https://discourse.processing.org/u/uGuigi)\
**Post date:** [November 14, 2022, 9:38pm UTC](https://discourse.processing.org/t/simple-text-input-program/26005/3 "2022-11-14T21:38:49Z")

</div>

Thank you, simple, clean and helpful 8^)
