How can I make my program 'wait' for my answer.

edited August 2017 in How To...

I want to ask a question with text() and make the program wait for my answer. Is there a command for making the program 'wait' for my key(keyPressed)?

Answers

  • noLoop will stop the automatic looping of draw ()

  • Umm... actually I didn't mean 'break' I want to make something like 'set /p a=' or 'choice /c ~' at batch kind of wait.(Asking a question and waiting for answer)

  • The draw() function in your sketch is always running. You need to continuously check the keyPresssed function and managed what is input. An example below.

    Kf

    //INSTRUCTIONS: New user input only after ENTER key is pressed. Only 
    //              alphanumeric characters accepted.
    
    boolean newUsrInput=false;
    String usrInput;
    String showStr="";
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
    
      if (newUsrInput==true) {
        showStr=usrInput;
    
        //NOW prepare for next user input
        usrInput="";
        newUsrInput=false;
      }
    
      background(0);
      text("User input= "+showStr, width/2, height/2);
    }
    
    
    
    void keyPressed() {
      if (key==ENTER) {
        newUsrInput=true;
      } else {
        if ( (key>='a'&key<='z') || (key>='0'&key<='9')  ) { 
          usrInput+=key;
        }
      }
    }
    

    Tag: usrInput

  • edited August 2017

    As has been said, there is no inbuilt method for this. You have to build your own.

    I use a state system for that where the program can be in different states.

    You can have an int state that tells you in which state you are. Initially it's 0, set it to 1 for waiting for input and to 2 when input is finished and then back to 0

Sign In or Register to comment.