keyCode

Examples

int fillVal = WHITE;
int y = 0;

void setup() {
  noLoop();
}

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

void keyPressed() {
  if (keyCode == UP) {
    y -= 5;
  } else if (keyCode == DOWN) {
    y += 5;
  } else if (keyCode == SELECT) {
    if(fillVal == WHITE) {
      fillVal = BLACK;
    } else {
      fillVal = WHITE;
    }
  }

  redraw();
}

Description

The variable keyCode is used to detect special keys (UP, DOWN, SELECT). If you use this variable, you have to define "ENABLE_KEY_EVENT" at settings.h and declare keyPressed() and keyReleased() at sketch.c.

Related

keyPressed()
keyReleased()

Back to index