Keyboard to input a word not just letter

This code takes a 1 letter input from keyboard and moves a pointer
what i would like to do is take a 4 letter word input something like

if (input == cold ) { do this stuff }

thankyou for your time

`Preformatted text`

PShape s;  // The PShape object
int y = 180; //pointer place
int rectWidth;

void setup() {
  size(640, 360, P3D);
  noStroke();
  background(0);
  rectWidth = width/4;
  s = createShape();
  s.beginShape();
  s.fill(255, 255, 255);
  s.noStroke();
  s.vertex(0, 0);
  //  s.vertex(0, 0);
  s.vertex(25, 25);
  s.vertex(50, 0);
  s.endShape(CLOSE);
}

void draw() {
  // keep draw() here to continue looping while waiting for keys

  textSize(50);
  fill(600, 600, 612);
  text("Temp", 40, 320);
  fill(0, 0, 600);
  rect(200, 290, 75, 30); //move accross,move up,size accross.size up
  fill(0, 600, 0);
  rect(275, 290, 100, 30); //move accross,move up,size accross.size up
  fill(600, 0, 0);
  rect(370, 290, 75, 30); //move accross,move up,size accross.size up

  textSize(20);
  fill(0, 0, 0);
  text("Cold", 210, 310);
  fill(0, 0, 0);
  text("Good", 300, 310);
  fill(0, 0, 0);
  text("Hot", 390, 310);
  fill(600, 600, 612);
}
void keyPressed() {
  if (keyPressed) {


    fill(0, 0, 0);
    rect(150, 250, 300, 30); //move accross,move up,size accross.size up

    if (key == 'z' ) {
      y = 180;
      shape(s, y, 255); //pointer
    }


    if (key == 'x' ) {
      y = 200;
      shape(s, y, 255); //pointer
    }
    if (key == 'c' ) {
      y = 220;
      shape(s, y, 255);//pointer
    }
  }
}

Hello @Dave,

Snippet of code to get you started:

String s = "";

void setup()
  {
  size(200, 200);
  }

void draw()
  {
  }
  
void keyPressed()
  {
  s = s + key;
  println(s);
  }

You can adapt the above for a word.

Take a look at references for:

Using keyPressed(), do you need to check if a key was pressed with keyPressed after a key was pressed? References should help with this.

:)

Hello @Dave,

Another hint:

void keyPressed()
  {
  s = s + key;
  if (s.equals("blue"))
    {
    word = s; // store word
    s = "";   //clear s
    }
  println("s:", s);
  println("word:", word);
  }

Also consider:

if(key == ENTER)

:)

Hi @Dave,

just as an example on how it could be done (as this can be challenging for beginners)…

Cheers
— mnse

// valid keywords
String[] keywords = {"cold", "good", "hot"};
// a buffer to keep the pressed keys
String tmpBuffer="";
// for debug
String message="";
// prevent unnecessary key repetition
String keyOn="";

void setup() {
  size(200, 200);
}

void keyPressed() {
  // dismiss if a key is already pressed or shift key is pressed
  if (!keyOn.isEmpty() || (key == CODED && keyCode == SHIFT))
    return;

  // only allow letters
  if (String.valueOf(key).matches("[a-zA-Z]")) {
    tmpBuffer+=key;
  }
  // backspace handling ... not really neccassary but nice to have
  else if (key==BACKSPACE) {
    if (tmpBuffer.length() > 0)
      tmpBuffer = tmpBuffer.substring(0, tmpBuffer.length()-1);
  }
  // prevent key repetition on several systems
  keyOn=String.valueOf(key);
  // check if a keyword is pressed
  checkForKeyWords();
}

void keyReleased() {
  // on key released
  if (keyOn.length() > 0 && keyOn.equalsIgnoreCase(key+""))
    keyOn="";
}

void checkForKeyWords() {
  boolean possible = false;
  for (int i = 0; i < keywords.length; i++) {
    // check if we hit a keyword
    if (tmpBuffer.equalsIgnoreCase(keywords[i])) {
      // trigger action
      wordDetected(keywords[i]);
      return;
    }
    // check if the matching still possible with the tmpBuffer content
    if (tmpBuffer.length() < keywords[i].length() && keywords[i].toLowerCase().startsWith(tmpBuffer.toLowerCase()))
      possible=true;
  }
  // if not possible, clear buffer and restart...
  if (!possible)
    tmpBuffer="";
}

void wordDetected(String word) {
  // clear buffer
  tmpBuffer="";
  // yay! we found a keyword
  message = "found: " + word;
  // do something depending on keyword
}

void draw() {
  background(0);
  fill(255);
  // show current tmpBuffer content
  text(tmpBuffer, 30, 30);
  // show last found keyword
  text(message, 30, 60);
}

thank you so much for your help

2 Likes