# How to ask a question and receive and answer

**URL:** https://discourse.processing.org/t/how-to-ask-a-question-and-receive-and-answer/10814
**Category:** Coding Questions
**Created:** [May 1, 2019, 4:58pm UTC](https://discourse.processing.org/t/how-to-ask-a-question-and-receive-and-answer/10814 "2019-05-01T16:58:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MichaelGuillet](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@MichaelGuillet](https://discourse.processing.org/u/MichaelGuillet)
#### Post date: [May 1, 2019, 4:58pm UTC](https://discourse.processing.org/t/how-to-ask-a-question-and-receive-and-answer/10814/1 "2019-05-01T16:58:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 1, 2019, 5:45pm UTC](https://discourse.processing.org/t/how-to-ask-a-question-and-receive-and-answer/10814/2 "2019-05-01T17:45:38Z")

</div>

Button

[https://processing.org/examples/button.html](https://processing.org/examples/button.html)

Keys

[https://processing.org/examples/keyboardfunctions.html](https://processing.org/examples/keyboardfunctions.html)

There are more examples

**Example**

(see also below)

you can get an input in the above example by

```auto
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!_

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 2, 2019, 10:59am UTC](https://discourse.processing.org/t/how-to-ask-a-question-and-receive-and-answer/10814/3 "2019-05-02T10:59:27Z")

</div>

example for key input:

```auto
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 
//

```
