Input more than one data fields from user with keyPressed

edited July 2014 in How To...

I am trying to modify the following code (from Amnon P5 website) to introduce four data fields from a user. The problem is that it's not possible to declare more than one string variable to be used it within keyPressed function because when I write a text in one of the field it is written at the same time the same text in all other fields. When I have defined new functions as dataField1, datafield2 it does not accept keyCode instructions, it appears a lot of small empty squares in the execution. I prefer not to use external processing GUI libraries for this if it is possible to avoid it. Any help will be very much appreciated

String myText = "Type something";

void setup() {
  size(500, 500);
  textAlign(CENTER, CENTER);

void draw() {
  text(myText, 0, 0, width, height);

void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (myText.length() > 0) {
      myText = myText.substring(0, myText.length()-1);
  } else if (keyCode == DELETE) {
    myText = "";
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
    myText = myText + key;


Sign In or Register to comment.