# \[SOLVED\] Using the keyboard to change the direction a moving circle

**URL:** <https://discourse.processing.org/t/solved-using-the-keyboard-to-change-the-direction-a-moving-circle/15991>\
**Category:** Coding Questions\
**Created:** [November 30, 2019, 6:03pm UTC](https://discourse.processing.org/t/solved-using-the-keyboard-to-change-the-direction-a-moving-circle/15991 "2019-11-30T18:03:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![P3t3R08](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@P3t3R08](https://discourse.processing.org/u/P3t3R08)\
**Post date:** [November 30, 2019, 6:03pm UTC](https://discourse.processing.org/t/solved-using-the-keyboard-to-change-the-direction-a-moving-circle/15991/1 "2019-11-30T18:03:52Z")

</div>

Hello,

I am trying to write a program that causes the circle to go in one direction but when I press the letter ‘a’ on the keyboard it will go the opposite direction. Additionally, if the circle reaches a boundary it will go the opposite direction. My issue is when I press the letter ‘a’ on my keyboard it only changes direction sometimes. I was trying to accomplish this by using an if statement. I have attached my code below. Any help would be greatly appreciated,

```auto
float x=300;
float y=400;

void setup() {
  size(850, 950);
  background(100);
}

void draw() {

  noStroke();

  ellipse(x, y, 50, 50);
  fill(0, random(155), random(255));
  x=x+Speed;

  if ((keyPressed == true) && (key == 'a')||x > width||x < 0) {
    Speed=Speed*-1;
  }

}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 30, 2019, 7:53pm UTC](https://discourse.processing.org/t/solved-using-the-keyboard-to-change-the-direction-a-moving-circle/15991/2 "2019-11-30T19:53:52Z")

</div>

Hello,

Welcome to community!

Consider the following:

1. Declare a global variable for Speed. The example does not do this.
2. One if statement for just the boundaries inside draw().
3. Using keyPressed():  
[keyPressed() / Reference / Processing.org](https://www.processing.org/reference/keyPressed_.html)

> The **keyPressed()** function is called **once** every time a key is pressed.

1. Use println() for seeing the status of variables in your code:  
[println() / Reference / Processing.org](https://processing.org/reference/println_.html)

This will demonstrate that key is detected **multiple** times:

```auto
  if ((keyPressed == true) && (key == 'a')||x > width||x < 0) {
    Speed=Speed*-1;
    print(char(key));
    }

```

🙂

---

<div class="post-metadata">

**Author:** ![P3t3R08](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@P3t3R08](https://discourse.processing.org/u/P3t3R08)\
**Post date:** [November 30, 2019, 8:28pm UTC](https://discourse.processing.org/t/solved-using-the-keyboard-to-change-the-direction-a-moving-circle/15991/3 "2019-11-30T20:28:24Z")

</div>

Hello,

Thank you for your help! The code is now working after implementing your suggestions. The biggest issue seems to have been that there was not a keyPressed () function.
