# KeyPressed doesn’t work

**URL:** https://discourse.processing.org/t/keypressed-doesn-t-work/16787
**Category:** Beginners
**Created:** [January 1, 2020, 8:38pm UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787 "2020-01-01T20:38:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 1, 2020, 8:38pm UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/1 "2020-01-01T20:38:02Z")

</div>

I’m pretty new to coding, and I have been trying to follow some of The Coding Train’s videos. But every video that he uses the keyPressed function, or anything that involves the keyboard in gameplay, nothing happens for me, like nothing moves when they are supposed to. I’m using a chrome book if that helps. If anybody can help me, that would be great.

---

<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: [January 1, 2020, 9:07pm UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/2 "2020-01-01T21:07:45Z")

</div>

Hey and welcome to the forum!

Do you use processing or p5?

Can you post your entire code please?

Please remember that everything is case sensitive in processing, so it’s setup and draw etc.

Regards, Chrisir

---

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 1, 2020, 9:45pm UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/3 "2020-01-01T21:45:27Z")

</div>

I’m using p5, and I know it sounds stupid, but how do I open a file?

---

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 1, 2020, 9:49pm UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/4 "2020-01-01T21:49:37Z")

</div>

Actually, I figured out how to open a file.  
I’m trying to code snake, it is not done, but in The Coding Train’s video, his is moving when he hits the arrow keys and mine isn’t. I’m pretty sure the code is the same though.  
Here it is:

this one is for my sketch.js file:  
var s;

function setup() {  
createCanvas(600, 600);  
s = new Snake();

}

function draw() {  
background(51);  
s.update();  
s.show();  
}

function keyPressed() {  
if (keyCode === UP\_ARROW) {  
s.dir(0, -1);  
} else if(keyCode == DOWN\_ARROW) {  
s.dir(0, 1);  
} else if(keyCode == RIGHT\_ARROW) {  
s.dir(1, 0);  
} else if(keyCode == LEFT\_ARROW) {  
s.dir(-1, 0);  
}  
}

This one is for my snake.js file:  
function Snake() {  
this.x = 0;  
this.y = 0;  
this.xspeed = 1;  
this.yspeed = 0;

this.dir = function(x, y) {  
this.xspeed = x;  
this.yspeed = y;  
}

this.update = function() {  
this.x = this.x + this.xspeed;  
this.y = this.y + this.yspeed;  
}

this.show = function() {  
fill(255);  
rect(this.x, this.y, 10, 10);

}  
}

---

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 2, 2020, 1:07am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/5 "2020-01-02T01:07:23Z")

</div>

does anybody have any idea on why that’s not working?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 2, 2020, 1:16am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/6 "2020-01-02T01:16:32Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

but actually, much more easy to play / help is you  
give us the link to your  
[https://editor.p5js.org](https://editor.p5js.org)  
project

* * *

if you make the snake function in a extra  
snake.js  
file, did you declare it in index.html?

---

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 2, 2020, 2:05am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/7 "2020-01-02T02:05:51Z")

</div>

[https://editor.p5js.org/Bronx\_Bombers/sketches/tx74QyQyY](https://editor.p5js.org/Bronx_Bombers/sketches/tx74QyQyY)  
yes, i put it in the index.html

---

<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: [January 2, 2020, 2:06am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/8 "2020-01-02T02:06:50Z")

</div>

It works here!  
After I click on the canvas with the mouse I can use the keyboard.

🙂

---

<div class="post-metadata">

### Author: ![All\_Rise](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@All\_Rise](https://discourse.processing.org/u/All_Rise)
#### Post date: [January 2, 2020, 2:08am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/9 "2020-01-02T02:08:12Z")

</div>

OMG! i feel like such an idiot! sorry to waste your time. It works

---

<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: [January 2, 2020, 7:57am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/10 "2020-01-02T07:57:37Z")

</div>

> [@glv](#):
>
> I click on the canvas with the mouse I can use the keyboard.

The program/ canvas didn’t have the focus initially, so we had to click the mouse

---

<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: [January 2, 2020, 11:58am UTC](https://discourse.processing.org/t/keypressed-doesn-t-work/16787/11 "2020-01-02T11:58:21Z")

</div>

> [@glv](#):
>
> After I click on the canvas with the mouse I can use the keyboard.

Some discussions on this topic:  
[[Automatically make sketch active after pressing play · Issue #654 · processing/p5.js-web-editor · GitHub](https://github.com/processing/p5.js-web-editor/issues/654)]  
[[Must click into canvas preview pane before key events are recognized · Issue #993 · processing/p5.js-web-editor · GitHub](https://github.com/processing/p5.js-web-editor/issues/993)]

I also found this example:

> **[p5.js Web Editor](https://editor.p5js.org/cassie/sketches/CQUX7SRTa)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

🙂
