# How can I set a move while hold a key?

**URL:** <https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346>\
**Category:** Beginners\
**Created:** [August 18, 2020, 11:55pm UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346 "2020-08-18T23:55:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![viniciusbrbio](https://avatars.discourse-cdn.com/v4/letter/v/b5a626/32.png) [@viniciusbrbio](https://discourse.processing.org/u/viniciusbrbio)\
**Post date:** [August 18, 2020, 11:55pm UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/1 "2020-08-18T23:55:53Z")

</div>

Hi everyone,

I made this little code and I want to hold the ‘d’ key to make the ball move to the right.

I want to move the ball until the edge of the screen just holding ‘d’ key. In the way that the code is, I need to press many times the button and I don’t want that. Please help me.

```auto
int x = 50;

void setup(){
size(100,600);
background(50);
}

void draw(){
background(50);
ellipse(x,y,24,24);
}

void keyPressed(){
 if(key == 'd'){
 x=x+2;
} 
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 12:06am UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/2 "2020-08-19T00:06:59Z")

</div>

have a boolean flag which is set when the key is pressed, and use said flag to determine whether or not you should be moving.

```auto
bPressed = false;

if(keyPressed & key = " someKey"){
bPressed= true;
}

if(!keyPressed){
bPressed = false;

}

if(bPressed) doSomething();

```

---

<div class="post-metadata">

**Author:** ![viniciusbrbio](https://avatars.discourse-cdn.com/v4/letter/v/b5a626/32.png) [@viniciusbrbio](https://discourse.processing.org/u/viniciusbrbio)\
**Post date:** [August 19, 2020, 12:26am UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/3 "2020-08-19T00:26:44Z")

</div>

> [@paulgoux](#):
>
> ```auto
> if(keyPressed & key = " someKey"){
> bPressed= true;
> }
> 
> if(!keyPressed){
> bPressed = false;
> 
> }
> 
> ```

Many thanks, but could you please put your example inside my code? I’m getting some errors here in the first line “bPressed = false;”. The error is: **expecting EOF, found ‘bPressed’**

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [August 19, 2020, 12:42am UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/4 "2020-08-19T00:42:03Z")

</div>

My code isn’t meant to run, its meant as an example for the logic.

Booleans need to be declared first;

```auto
boolean a = false;

```

^ writing boolean before a variable name declares it as a boolean. This is the same for all other variable types.

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [August 19, 2020, 3:26am UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/5 "2020-08-19T03:26:02Z")

</div>

instead of

```auto
if(key == 'd'){
x=x+2;
}

```

i think it should be

```auto
if(key == 'd'){
x+=2;
}

```

---

<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 19, 2020, 5:15am UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/6 "2020-08-19T05:15:39Z")

</div>

> **[r/processing - How would I make a keyPressed function support multiple keys?](https://www.reddit.com/r/processing/comments/bshqa8/how_would_i_make_a_keypressed_function_support/eor5epo/)**
>
> 2 votes and 13 comments so far on Reddit

https://www.openprocessing.org/sketch/718355/embed/

---

<div class="post-metadata">

**Author:** ![viniciusbrbio](https://avatars.discourse-cdn.com/v4/letter/v/b5a626/32.png) [@viniciusbrbio](https://discourse.processing.org/u/viniciusbrbio)\
**Post date:** [August 19, 2020, 12:05pm UTC](https://discourse.processing.org/t/how-can-i-set-a-move-while-hold-a-key/23346/7 "2020-08-19T12:05:52Z")

</div>

Wonderful! Many thanks
