If keyPressed in processing
<p>I'm trying to make a simple program to calculate loan interest. The code I have here works but will only display the result when the 'c' key is pressed down. How can i change the logic so that once 'c' is pressed the result appears on the screen. Any help is appreciated in advanced.</p>



<pre class="lang-java prettyprint-override"><code>   import controlP5.*;
ControlP5 cp5;
Textfield loan; 
Textfield interest;
Textfield months;
Button calculate;
Double loan1;
Double months1;
double interest1;
void setup(){
size(700,400);
PFont font = createFont("arial",20);
cp5 = new ControlP5(this);
loan =  cp5.addTextfield("Loan Amount")
 .setPosition(20,100)
 .setSize(200,40)
 .setFont(font)
 .setFocus(true)
 .setColor(color(255,0,0));
 loan.setInputFilter(ControlP5.INTEGER)
 ;         
  interest =  cp5.addTextfield("Interest Rate")
 .setPosition(20,190)
 .setSize(200,40)
 .setFont(font)
 .setFocus(false)
 .setColor(color(255,0,0));
 interest.setInputFilter(ControlP5.INTEGER)
 ;
  months =  cp5.addTextfield("Length in months")
 .setPosition(450,100)
 .setSize(200,40)
 .setFont(font)
 .setFocus(false)
 .setColor(color(255,0,0));
 months.setInputFilter(ControlP5.INTEGER)
 ;
  }
 void draw(){
background(120);
fill(255);
noStroke();
rect(0,0,750,70);
fill(20);
textSize(32);
text("Monthly Loan Repayment Calculator ", 70, 45); 
fill(41,194,214);
ellipse(340,170,130,130);
fill(24,74,140);
ellipse(340,170,125,125);
fill(255);
textSize(18);
text("Enter 'c'",305,155);
text("to calculate", 295,175);
fill(0);
rect(0,260,700,1);

String loan2 =     loan.getText();
String interest2 = interest.getText();
String months2 =   months.getText();

 if(keyPressed)
{
    if(key=='c'||key=='C'&amp;&amp;!loan2.equals("")&amp;&amp;!interest2.equals("")&amp;&amp;!months2.equals         ("")){
loan1 = Double.parseDouble(loan2);
interest1 = Double.parseDouble(interest2);
months1 = Double.parseDouble(months2);

interest1=interest1/100/12;
double payment = (loan1*interest1)/(1-Math.pow(interest1+1,-months1));
payment = (double)Math.round(payment*100)/100;
//println(payment);
//text(payment,20,20);

int total = (int)payment;

text(total,200,350);
  }


}
}
</code></pre>