# Keypressed Function Stuck On

**URL:** <https://discourse.processing.org/t/keypressed-function-stuck-on/47341>\
**Category:** Coding Questions\
**Created:** [October 13, 2025, 4:12pm UTC](https://discourse.processing.org/t/keypressed-function-stuck-on/47341 "2025-10-13T16:12:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [October 13, 2025, 4:12pm UTC](https://discourse.processing.org/t/keypressed-function-stuck-on/47341/1 "2025-10-13T16:12:49Z")

</div>

\<  
float playerX=width/2;  
float playerY=height/2;  
float playerSpeedXPositive = 0;  
float playerSpeedXNegative = 0;  
float playerSpeedYPositive = 0;  
float playerSpeedYNegative = 0;  
final int \_keyD = 40;  
final int \_keyW = 87;  
final int \_keyS = 83;  
final int \_keyA = 65;  
final int \_keySP = 32;  
boolean opening = true;  
boolean playing = false;  
boolean dead = false;  
boolean keyDwn = new boolean[7];  
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||SETUP|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  
void setup(){  
size(600,600);  
background(0);  
windowResize(displayWidth/2, displayHeight/2);  
keyDwn[0] = false;  
keyDwn[1] = false;  
keyDwn[2] = false;  
keyDwn[3] = false;  
keyDwn[4] = false;  
}  
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||DRAW|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  
void draw(){  
background(0);  
rectMode(CENTER);  
fill(255);  
circle(playerX,playerY,width/16);  
fill(0);  
circle(playerX,playerY,width/22);  
if (keyDwn[0] = true) {  
background(255);  
playerSpeedYNegative = playerSpeedYNegative + width/100;  
playerY = playerY-playerSpeedYNegative;  
} else {  
background(0);  
}  
if (keyDwn[2] = true) {  
playerSpeedYPositive = playerSpeedYPositive + width/100;  
playerY = playerY+playerSpeedYPositive;  
}  
}  
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||KEYPRESSED||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  
void keyPressed() {  
if (keyCode == \_keyW) {  
keyDwn[0] = true;  
}  
if (keyCode == \_keyA) {  
keyDwn[1] = true;  
}  
if (keyCode == \_keyS) {  
keyDwn[2] = true;  
}  
if (keyCode == \_keyD) {  
keyDwn[3] = true;  
}  
if (keyCode == \_keySP) {  
keyDwn[4] = true;  
}  
}  
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||KEYRELEASED|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  
void keyReleased() {  
if (keyCode == \_keyW) {  
keyDwn[0] = false;  
}  
if (keyCode == \_keyA) {  
keyDwn[1] = false;  
}  
if (keyCode == \_keyS) {  
keyDwn[2] = false;  
}  
if (keyCode == \_keyD) {  
keyDwn[3] = false;  
}  
if (keyCode == \_keySP) {  
keyDwn[4] = false;  
}  
}

> 

The keypressed function seems stuck on but also off(?)

I added the thing that changes the background to white for when it’s on, but the background stays white even if PlayerY Doesn’t change.

Any way to fix this?

---

<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:** [October 13, 2025, 4:33pm UTC](https://discourse.processing.org/t/keypressed-function-stuck-on/47341/2 "2025-10-13T16:33:58Z")

</div>

Hello @WillyGrz,

Please read:

- _[Welcome to the Processing Foundation Discourse](https://discourse.processing.org/t/welcome-to-the-processing-foundation-discourse/8)_

- _[Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147)_

Your code is not formatted correctly.

This is certainly not correct and there are other instances of this:

> [@WillyGrz](#):
>
> if (keyDwn[2] = true) {

Reference:

> **[== (equality) / Reference](https://processing.org/reference/equality.html)**
>
> Determines if two values are equivalent. Please note the equality operator (==) is different from the assignment operator (=) and although they look similar, they have a different use. If you're compa…

`:)`

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [October 13, 2025, 4:44pm UTC](https://discourse.processing.org/t/keypressed-function-stuck-on/47341/3 "2025-10-13T16:44:15Z")

</div>

> [@WillyGrz](#):
>
> if (keyDwn[0] = true) {

This is your problem

`keyDwn[0] = true`

is an _ **assignment statement** _ and in this case stores the value `true` in the variable `keyDwn[0]` and it also returns the assigned value which in this case is `true` so it seems to be ‘stuck on’

If you want to _ **test / compare** _ the value stored in `keyDwn[0]` then you need to use the `==` operator so the above statement should be

`if (keyDwn[0] == true) {`

---

<div class="post-metadata">

**Author:** ![WillyGrz](https://avatars.discourse-cdn.com/v4/letter/w/c77e96/32.png) [@WillyGrz](https://discourse.processing.org/u/WillyGrz)\
**Post date:** [October 13, 2025, 4:51pm UTC](https://discourse.processing.org/t/keypressed-function-stuck-on/47341/4 "2025-10-13T16:51:27Z")

</div>

Ohh okay thank you lol, I just came back from a long hiatus and kind of forgot how booleans work
