How to ask a question and receive and answer

I am coming from a background in coding in scratch i know how lame that sounds but i have a couple of questions on how to ask a question like you can in scratch and set the answer to a variable. Also how to make clickable buttons, i am currently working on a calculator and would like to know how to do these things to make my life easier when coding.

2 Likes

Button

https://processing.org/examples/button.html

Keys

https://processing.org/examples/keyboardfunctions.html

There are more examples

Example

(see also below)

you can get an input in the above example by

result = result + key;

if (key==RETURN || key==ENTER) {
     // input is entered, proceed with next variable result2
}

I like Scratch and I think processing is useful to go on from Scratch, so welcome!

2 Likes

example for key input:

byte  leftMargin = 10;
byte  buffMax = 5;
color BG  = 255, FG = 0;

String buff = "";
String result = "";

int state=0; 

void setup() {
  size(300, 600);
  background(BG);
}

void draw() {

  background(BG);

  if (state==0) {
    // state for input
    fill(FG);
    text(buff, leftMargin, 20);
    stroke( frameCount % 30 < 30 >>2 ? BG : FG );
    int rPos = (int) textWidth(buff) + leftMargin +1; 
    line(rPos, 9, rPos, 23);
  } else if (state ==1) {
    // state after input
    text (result, 23, height-23);
  }
}

void keyPressed() {
  int len = buff.length();

  if (key==CODED) { 
    return;
  }

  if (key == RETURN || key == ENTER ) {
    state=1; 
    result=buff; 
    buff="";
  } else if (key == BACKSPACE) {
    // delete last letter 
    if ( len > 0 ) {
      buff = buff.substring(0, len-1);
    }
  } else {
    // normal input 
    buff = buff + key;
  }
} // func 
//
2 Likes