How do I program the arrow keys to move object instead of mouseX and mouseY?


#1
import processing.video.*;
Capture cam;

void setup() {
  size(560, 360);
  cam = new Capture(this, 560, 360, 30);
  cam.start();
}

void draw() {
  if (cam.available()) {
    cam.read();


    loadPixels();
    cam.loadPixels();  

    for (int x = 0; x < cam.width; x++) {    
      for (int y = 0; y < cam.height; y++) {      
        // Calculate the 1D location from a 2D grid
        int loc = x + y * cam.width;      

        // Get the red, green, blue values from a pixel      
        float r = red  (cam.pixels[loc]);      
        float g = green(cam.pixels[loc]);      
        float b = blue (cam.pixels[loc]);      

        // Calculate an amount to change brightness based on proximity to the mouse      
        float d = dist(x, y, mouseX, mouseY);      
        float adjustbrightness = map(d, 0, 50
          , 4, 0);      
        r *= adjustbrightness;      
        g *= adjustbrightness;      
        b *= adjustbrightness;      

        // Constrain RGB to make sure they are within 0-255 color range      
        r = constrain(r, 0, 255);      
        g = constrain(g, 0, 255);      
        b = constrain(b, 0, 255);      

        // Make a new color and set pixel in the window      
        color c = color(r, g, b);      
        pixels[loc] = c;
      }
    }  

    updatePixels();
  }
}


#2

Well, you can use the function keyPressed, and declare a global variable who stores the x and y position of the object, like the follows:

int posX = 0;
int posY = 0;

void setup(){
}
void draw(){
yourObjectPos(posX, posY);
}

void keyPressed(){

if(keyCode == LEFT){
posX -= 5;
 }

if(keyCode == RIGHT){
posX += 5;
 }
}


and the same for UP and DOWN, hope it helps!


#3

and @bryan a method i use to have continuous press is this:

int[] keys = new int[400];//400 is an exaggerated guess

void keyPressed(){
keys[keyCode];
}
void keyReleased(){
keys[keyCode] = false;
}
if(keys[LEFT]){
posX -= 5;
 }

if(keys[RIGHT]){
posX += 5;
 }

#5

yeah, this looks great!, continuous is way better