# Need help with the keyPressed () function

**URL:** <https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863>\
**Category:** Coding Questions\
**Created:** [September 15, 2020, 5:03am UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863 "2020-09-15T05:03:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Rogue12](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@The\_Rogue12](https://discourse.processing.org/u/The_Rogue12)\
**Post date:** [September 15, 2020, 5:03am UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863/1 "2020-09-15T05:03:31Z")

</div>

So I am trying to write code that changes a variable when I press a certain key. The problem is, the keyPressed() function isn’t detecting any key presses. What am I doing wrong? My code:

```auto
var x, y;
var shape;

function setup() {
  createCanvas(400, 400);
  x = 0;
  y = 0;
  shape = 0;
}
function draw() {
  background(0, 0, 0, 20);
  if (shape == 0) {
    followShape0();
  }
  if (shape == 1) {
    followShape1();
  }
}
function keyPressed() {
  if (key == 'a') {
    shape = 1;
    print('a');
  }
if (key == 'b') {
    shape = 0;
    print('b');
  }
}
function followShape0() {
  ellipse(x, y, 20, 20);
}
function followShape1() {
  beginShape();
  vertex(x, y);
  vertex(x - 20, y);
  vertex(x - 20, y - 20);
  vertex(x, y - 20);
  endShape(CLOSE);
}
function mouseMoved() {
  x = mouseX;
  y = mouseY;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 15, 2020, 6:13am UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863/2 "2020-09-15T06:13:13Z")

</div>

> [@The\_Rogue12](#):
>
> the keyPressed() function won’t work no matter where I call it.

You don’t have to call it

It starts automatically

---

<div class="post-metadata">

**Author:** ![The\_Rogue12](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@The\_Rogue12](https://discourse.processing.org/u/The_Rogue12)\
**Post date:** [September 15, 2020, 6:27am UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863/4 "2020-09-15T06:27:02Z")

</div>

> [@Chrisir](#):
>
> You don’t have to call it
> 
> It starts automatically

Sorry, I meant that the function won’t detect my key presses, no matter where I put the function.

---

<div class="post-metadata">

**Author:** ![yanze03](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@yanze03](https://discourse.processing.org/u/yanze03)\
**Post date:** [September 15, 2020, 6:04pm UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863/5 "2020-09-15T18:04:42Z")

</div>

try:

```auto
if(keyPressed && key == 'a'){}

```

---

<div class="post-metadata">

**Author:** ![The\_Rogue12](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@The\_Rogue12](https://discourse.processing.org/u/The_Rogue12)\
**Post date:** [September 15, 2020, 6:22pm UTC](https://discourse.processing.org/t/need-help-with-the-keypressed-function/23863/6 "2020-09-15T18:22:05Z")

</div>

> [@yanze03](#):
>
> try:
> 
> ```auto
> if(keyPressed && key == 'a'){}
> 
> ```

This worked! Thank you.
