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

**URL:** <https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267>\
**Category:** Libraries\
**Created:** [December 3, 2018, 9:26pm UTC](https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267 "2018-12-03T21:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alxsnol](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@alxsnol](https://discourse.processing.org/u/alxsnol)\
**Post date:** [December 3, 2018, 9:26pm UTC](https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267/1 "2018-12-03T21:26:38Z")

</div>

```auto
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();
  }
}

```

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [December 4, 2018, 12:09am UTC](https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267/2 "2018-12-04T00:09:19Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)\
**Post date:** [December 4, 2018, 12:30am UTC](https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267/3 "2018-12-04T00:30:39Z")

</div>

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

```auto
boolean[] keys = new boolean[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;
 }

```

---

<div class="post-metadata">

**Author:** ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)\
**Post date:** [December 4, 2018, 12:40am UTC](https://discourse.processing.org/t/how-do-i-program-the-arrow-keys-to-move-object-instead-of-mousex-and-mousey/6267/5 "2018-12-04T00:40:00Z")

</div>

yeah, this looks great!, continuous is way better
