如何让keyCode == ENTER更新字符串?

时间:2016-04-17 09:24:27

标签: variables processing enter weather

我的程序在这里,你可以看到我有一个名为“S”的字符串,以及一个无效的Get Temperature。在处理按键的底部,它有一个带有ENTER的else If语句。我想要它,这样当你按Enter键时,它会将字符串更新为你输入的内容,然后将其加载到“SetAddress”字段中。我该怎么做呢?

 
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 = "Enter Location";
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 (30);
}

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() {

  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) {
    } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && s.length() < 20) { //It's an ok char, add it to the String
      s += key;
    }
  }
}

1 个答案:

答案 0 :(得分:0)

好吧,看起来你正在存储用户在s变量中输入的内容。然后,您将获得if语句的这一部分:

 
else if (keyCode == ENTER && s.length() != 0) {
}

只要用户在输入内容后按Enter键,就会输入else if。因此,您只需将所需的任何代码放在该语句的正文中:

else if (keyCode == ENTER && s.length() != 0) {
   getTemperatureChoreo.setAddress(s);
}

我可能不完全理解你的代码,所以这只是一个例子。但基本思路是一样的:当用户按下Enter键时执行某些操作,只需将您想要的代码放在此else if块中。

将来,请尝试发布MCVE而不是整个草图。例如,您的问题与minim无关,因此您可以摆脱所有代码。从空白草图重新开始,只添加足够的代码,以便我们可以复制并粘贴它以自行运行它以查看您遇到的位置。祝你好运。