String lastInput = new String();
String currentInput = new String();
float y = 10.0;
PFont myFont;
void setup()
{
size(200, 600);
smooth();
myFont = createFont("FFScala", 14);
textFont(myFont);
// textAlign(LEFT);
background(255, 255, 255);
fill(100);
noStroke();
}
void draw(){
ellipse (100, 100, y, y);
}
void keyPressed() {
if (key == ENTER) {
lastInput = currentInput;
currentInput = "";
background(255,255,255);
}
else if (key == BACKSPACE && currentInput.length() > 0) {
currentInput = currentInput.substring(0, currentInput.length() - 1);
}
else if (key == CODED) {
// ignore...
}
else {
currentInput = currentInput + key;
}
background(255,255,255);
text("Final value: " + lastInput, 2, height*.85);
y = changeNumber(lastInput);
println(y);
text("Input here: " + currentInput, 2, height*.75);
}
float changeNumber(String theText) {
float n = float(theText);
if(!Float.isNaN(n)) {
return n;
}
else {
println("not a number ");
return 0.0;
}
}
Applet 55 - Getting Text Input
In order to get text or numerical input from the user you will need to use a method that runs whenever the user inputs a character from the keyboard. This code does just that.
The changeNumber method returns a float if the user inputs a number, otherwise it returns a 0.0.
In this examle numerical input controls the size of a circle.