How to make keyCode == ENTER update string?

I have my programme here, where you can see I have a string called "S", and a void Get Temperature. At the bottom where keypresses are processed, it has an else If statement with ENTER. I want it so that when you press enter, it updates the string (s) to whatever you have typed, and then load it into the "SetAddress" field. How would I go about this?

    import com.temboo.core.*;
    import com.temboo.Library.Yahoo.Weather.*;
    import ddf.minim.*;
    AudioPlayer player;
    Minim minim;
    PImage bg;
    String myText;
    PFont Bold;
    PFont Thin;
    TembooSession session = new TembooSession("goldsmiths-c", "myFirstApp", "CNgLbwqnqzGdsnk6wHXPfAnQNSmV0Fmr");
    String s = "London";
    int prev = frameCount;
    //KeyPressed KeyPressed = new KeyPressed();

    void setup() {
      size(960, 540);
      bg = loadImage("mountains.jpg");
      minim = new Minim(this);
      player = minim.loadFile("song.mp3");
      player.play();
      player.loop();
      runGetTemperatureChoreo(); 
      Bold = createFont ("TTFirsBlackItalic.otf", height);
      Thin = createFont ("TTFirsThin.otf", height);
      frameRate (10);
    }

    void draw() {
      background(bg);
      fill (0);
      textFont (Bold);
      textSize (48);
      fill(255, 255, 255);
      text(myText, 10, 390);
      fill(255, 255, 255);
      textFont (Thin);
      textSize (48);
      text(s, 10, 500);
      print(mouseY);
    }

    void runGetTemperatureChoreo() {
    loop();
      GetTemperature getTemperatureChoreo = new GetTemperature(session);

      getTemperatureChoreo.setAddress(s);
      getTemperatureChoreo.setUnits("c");

      GetTemperatureResultSet getTemperatureResults = getTemperatureChoreo.run();
      myText = (s) + (getTemperatureResults.getTemperature() + ("°c"));
      print(getTemperatureResults.getTemperature());
    }
    void keyPressed()
    {
      if (keyPressed && prev <= frameCount-10) { //If a key is being pressed, and the security/delay is fine with it
        prev = frameCount; //re-Init the clock
        if (keyCode == BACKSPACE) { //Delete a char!
          if (s.length() > 0) {
            s = s.substring(0, s.length()-1);
          }
        } else if (keyCode == DELETE) {
          s = "";
        } else if (keyCode == ENTER && s.length() != 0) {
          s += key;
        } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && s.length() < 20) { //It's an ok char, add it to the String
          s += key;
        }
      }
    }

Answers

Sign In or Register to comment.