Home

Video: Keyboard input 1

Notes for this video are found below.

Complete program

int x = 200;
int y = 200;

void setup()
{
  size(400, 400);
}

void draw()
{
  background(255);
  fill(0);
  ellipse(x, y, 50, 50);
}

void keyPressed()
{
  println(key);
  if(key == 'w') // up
  {
    y = y - 10;
  }
  if(key == 's') // down
  {
    y = y + 10;
  }
  if(key == 'a') // left
  {
    x = x - 10;
  }
  if(key == 'd') // right
  {
    x = x + 10;
  }
  
  if(key == CODED)
  {
    if(keyCode == UP)
    {
      y = y - 10;
    }
    if(keyCode == DOWN)
    {
      y = y + 10;
    }
    if(keyCode == LEFT)
    {
      x = x - 10;
    }
    if(keyCode == RIGHT)
    {
      x = x + 10;
    }
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.