help (keypressed)

edited November 2015 in Using Processing

how can i use keypressed to press a specific key like "s"

Answers

  • You would have to press the key yourself, but the variable key always stores the last key that has been pressed. Example:

    void draw(){}
    void keyPressed(){
      if(key == 's'){
        println("'s' pressed");
      }
    }
    
  • edited October 2015

    If you're only interested in knowing whether the 'S' key was pressed, use keyCode:

    void keyPressed() {
      final k = keyCode;
      if (k == 'S')  println("Either 'S' or 's' was pressed");
    }
    

    However, if you need to distinguish the lower & upper cases, use key instead:

    void keyPressed() {
      if      (key == 'S')  println('S');
      else if (key == 's')  println('s');
    }
    
Sign In or Register to comment.