# keyCode repeats forever

**URL:** <https://discourse.processing.org/t/keycode-repeats-forever/1693>\
**Category:** Coding Questions\
**Created:** [July 11, 2018, 5:03pm UTC](https://discourse.processing.org/t/keycode-repeats-forever/1693 "2018-07-11T17:03:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NinjaKili](https://avatars.discourse-cdn.com/v4/letter/n/aeb1de/32.png) [@NinjaKili](https://discourse.processing.org/u/NinjaKili)\
**Post date:** [July 11, 2018, 5:03pm UTC](https://discourse.processing.org/t/keycode-repeats-forever/1693/1 "2018-07-11T17:03:15Z")

</div>

float brush\_x = 10;  
float brush\_y = 10;

void setup() {  
size(800,800);  
background(0);  
rectMode(CENTER);  
frameRate(300);  
}

void draw() {  
if(mousePressed == true) {  
noStroke();  
ellipse(mouseX,mouseY,brush\_x,brush\_y);  
}  
if(keyCode == DOWN) {  
brush\_x = brush\_x - 1;  
brush\_y = brush\_y - 1;  
} else  
if(keyCode == UP) {  
brush\_x = brush\_x + 1;  
brush\_y = brush\_y + 1;  
}

}

This is my code and whenever I press the up/down arrow it adds 1 to the size every frame even if I release it it still keeps going. Does anyone know how to fix this?

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 11, 2018, 5:05pm UTC](https://discourse.processing.org/t/keycode-repeats-forever/1693/2 "2018-07-11T17:05:33Z")

</div>

The `keyCode` variable holds the most recently pressed key, whether that key is still pressed or not. You might want to also check the `keyPressed` variable as well:

```auto
if(keyPressed){
 if(keyCode == DOWN) {
  brush_x = brush_x - 1;
  brush_y = brush_y - 1;
 } else
 else if(keyCode == UP) {
  brush_x = brush_x + 1;
  brush_y = brush_y + 1;
 }
}

```
