user3718913
user3718913

Reputation: 11

Using the same key for different if statements in the keypressed function - processing

I'm actually using Processing to check for entered values from the keyboard and take actions. Now the problem is that i would like to use the number "1" on the keyboard to take the two different action depending on an IF statement but the second condition doesn't seem to work. Pls, help me peruse this code as i don't know where i may be going wrong

void keyPressed() {

  if(page=="buttons")
{  
       if(key == '1') {
       text("This is the button page1", 30, 200);

                       }

       if(key == '2') {
       text("This is the button page2", 30, 200);

        }


else if(page=="options")
{  

      if (key == '1') {
      text("This is the options page", 100, 200);

  }
}

Upvotes: 0

Views: 547

Answers (1)

Fabricator
Fabricator

Reputation: 12782

There's a missing } before the else if. Also string comparison should be done with .equals instead of ==. Here's the fixed one

void keyPressed() {

    if(page.equals("buttons")) {  
        if(key == '1') {
            text("This is the button page1", 30, 200);

        } else if(key == '2') {
            text("This is the button page2", 30, 200);

        }
    } else if(page.equals("options")) {  

        if (key == '1') {
            text("This is the options page", 100, 200);

        }
    }
}

Upvotes: 1

Related Questions