Search processing.org:

Reference for Processing (BETA) version 0135+. If you have a previous version, use the reference included with your software. If you see any errors or have any comments, let us know.

Name | ### keyCode
Examples |
    int x = 90;

    void draw() {
      noStroke();
      rect(x, 10, 3, 80);
    }

    void keyPressed() {
      if (keyCode == BACKSPACE) {
        x = x - 5;
      }
    }
    color fillVal = color(126);

    void draw() {
      fill(fillVal);
      rect(25, 25, 50, 50);
    }

    void keyPressed() {
      if (key == CODED) {
        if (keyCode == UP) {
          fillVal = 255;
        } else if (keyCode == DOWN) {
          fillVal = 0;
        }
      } else {
        fillVal = 126;
      }
    }
Description | The system variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT, BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE. When checking for UP, DOWN, LEFT, RIGHT, ALT, CONTROL, and SHIFT, it's first necessary to check and see if the key is coded. This is done with the conditional "if(key == CODED) {}" as shown in the second example. The keys included in the ASCII specification (BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE) do not require checking to see if they key is coded. If you're making cross-platform projects, note that the ENTER key is commonly used on PCs and Unix and the RETURN key is used instead on Macintosh. Check for both ENTER and RETURN to make sure your program will work for all platforms.
Syntax |
    keyCode
Parameters |
Usage | Web & Application
Related | key keyPressed keyPressed() keyReleased()

Updated on May 11, 2006 03:45:55am PDT