# Trouble with keyCode

**URL:** <https://discourse.processing.org/t/trouble-with-keycode/13376>\
**Category:** Coding Questions\
**Created:** [August 14, 2019, 2:16pm UTC](https://discourse.processing.org/t/trouble-with-keycode/13376 "2019-08-14T14:16:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [August 14, 2019, 2:16pm UTC](https://discourse.processing.org/t/trouble-with-keycode/13376/1 "2019-08-14T14:16:11Z")

</div>

Hi, so I’m trying to move a square around the screen using the arrow keys. I have the code written for the square to move right when the right arrow key is pressed, but the square keeps moving once the key is released. How do I make the square stop once the right arrow key is released?

Thanks in advance 🙂

```auto
float x, y;

void setup() {
  size(600, 600);
  noStroke();
  x = width/2;
  y = height/2;
}

void draw() {
  background(0);
  rect(x, y, 20, 20);
  
  if (key == CODED) {
    if (keyCode == RIGHT) {
      x = x + 1;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [August 14, 2019, 2:26pm UTC](https://discourse.processing.org/t/trouble-with-keycode/13376/2 "2019-08-14T14:26:18Z")

</div>

There are built-in methods which called when the key is pressed or released  
[https://processing.org/reference/keyPressed\_.html](https://processing.org/reference/keyPressed_.html)  
[https://processing.org/reference/keyReleased\_.html](https://processing.org/reference/keyReleased_.html)  
add these method to your code to override the built-in methods  
you can use these to handle the situation, something like thiz

```auto
void keyPressed(){
    if(key == CODED){
     if(keyCode == RIGHT){
       moving();
     }
  }
}

void keyReleased(){
  if(key == CODED){
     if(keyCode == RIGHT){
       stopMoving();
     }
  }
}

```

if you bother to use these methods, another way is to use global variable named `keyPressed` (boolean) which tell if any key being pressed, just modify the code lil bit

```auto
if(keyPressed){
 if (key == CODED) {
     if (keyCode == RIGHT) {
     x = x + 1;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![abigail](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@abigail](https://discourse.processing.org/u/abigail)\
**Post date:** [August 14, 2019, 2:48pm UTC](https://discourse.processing.org/t/trouble-with-keycode/13376/3 "2019-08-14T14:48:57Z")

</div>

Thanks very much @humayung ! 😁

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 14, 2019, 6:51pm UTC](https://discourse.processing.org/t/trouble-with-keycode/13376/4 "2019-08-14T18:51:47Z")

</div>

- [Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp](http://Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp)
- [Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item\_6](http://Forum.Processing.org/two/discussion/8511/movement-code-messed-up#Item_6)
