# Is there any way to tell if any key is pressed

**URL:** <https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572>\
**Category:** CS Discoveries\
**Tags:** gamelab\
**Created:** [December 12, 2021, 5:22am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572 "2021-12-12T05:22:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 12, 2021, 5:22am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/1 "2021-12-12T05:22:24Z")

</div>

I’m currently trying to find a way if any key in general is pressed.  
I can use if(keyWentDown(“a”))  
But that only checks for the a key  
Is there a way to tell if any key on the keyboard is pressed without just making an array of all the characters on a keyboard and looping through them.?

---

<div class="post-metadata">

**Author:** ![rossiai](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/rossiai/32/7776_2.png) [@rossiai](https://forum.code.org/u/rossiai)\
**Post date:** [December 12, 2021, 9:23pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/2 "2021-12-12T21:23:16Z")

</div>

Hello. I have to try a few things, before I can tell you… But in the meantime… would it help to use a negative statement? I mean… if is not “p” do this or that… ​This will cover everything in the keyboard except for “p”

Also, I feel that you might not really need an array. You could hard code a conditional statement: if (keyWentDown(“down”) || keyWentDown(“a”)) || keyWentDown (“b”)). etc.

Just brainstorming.

Is this for Computer Discoveries or Computer Science Principles?

Ale

---

<div class="post-metadata">

**Author:** ![rossiai](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/rossiai/32/7776_2.png) [@rossiai](https://forum.code.org/u/rossiai)\
**Post date:** [December 13, 2021, 3:26am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/3 "2021-12-13T03:26:19Z")

</div>

Again, can you use a negative conditional with a non English character? Something like:

if (!keyDown(“ñ”))

It should work with all of the keys in the English keyboard.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 13, 2021, 3:27am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/4 "2021-12-13T03:27:03Z")

</div>

Well, this solution is kind of like making an array, but the key codes lie in the range of 8 to 222 so knowing that, you don’t really have to make an array to loop through them.

I can verify that this works:

```auto
var count = 0;
function draw() {
  for (var i = 8; i < 223; i++) {
    if (keyWentDown(i)) {
      count = count+1;
    }
  }
}

```

I am not aware of another way, but if someone else has an idea, that would be great! I’d be curious to know as well.

Mike

---

<div class="post-metadata">

**Author:** ![rossiai](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/rossiai/32/7776_2.png) [@rossiai](https://forum.code.org/u/rossiai)\
**Post date:** [December 13, 2021, 3:28am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/5 "2021-12-13T03:28:33Z")

</div>

Why you don’t want to make an array?

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 13, 2021, 3:29am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/6 "2021-12-13T03:29:52Z")

</div>

Except, wouldn’t that be true if no key at all was pressed?

Mike

---

<div class="post-metadata">

**Author:** ![rossiai](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/rossiai/32/7776_2.png) [@rossiai](https://forum.code.org/u/rossiai)\
**Post date:** [December 13, 2021, 4:02am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/7 "2021-12-13T04:02:26Z")

</div>

Can you add a second condition? (!keyDown(“ñ”)) && (!keyDown(“ ”))?

Can you share your app and explain what is it that you are trying to accomplish?

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 13, 2021, 2:50pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/8 "2021-12-13T14:50:53Z")

</div>

Making a sort of terminal for a game to get user input.

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 13, 2021, 3:19pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/9 "2021-12-13T15:19:39Z")

</div>

I tried that but my issue ends up becoming that it will print any character, including control characters like or , so I then have to write like 25 seperate if statements to prevent/get rid of them.

(each control character is different so I can’t just get rid of them all with one str.replace())

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 13, 2021, 3:32pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/10 "2021-12-13T15:32:38Z")

</div>

It’s kind of hard to visualize without seeing the code and how you are doing it. I’m not quite sure what you mean by “print any character”. The code I shared will detect any key press, but sounds like you are going beyond just detecting key presses.

Mike

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 13, 2021, 5:31pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/11 "2021-12-13T17:31:51Z")

</div>

Well it’s printing control characters. (they got automatically deleted in the message I sent, I’ll attach a photo). How do I link my project exactly? I apologize, I’m quite new to this.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 13, 2021, 5:45pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/12 "2021-12-13T17:45:51Z")

</div>

Look for the “Share” link in the upper left hand corner of the screen and get the “shared” link.

Mike

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 13, 2021, 6:03pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/13 "2021-12-13T18:03:45Z")

</div>

> **[Check out what I made](https://studio.code.org/projects/gamelab/b5JWXleryMKxfB1dV2cL-fxMtAXUJWyAbbtGssmnW6U)**
>
> I wrote the code myself with Code.org

Just a test thing. Ignore the polyfill stuff, the code thats important is line 34 and 35. Run it and press ctrl and alt and you’ll see the problem

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 13, 2021, 6:38pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/14 "2021-12-13T18:38:18Z")

</div>

It wouldn’t run as is, so I had to comment out some lines, but I can see what you are saying. It DOES detect any key being pressed, but you were hoping to only include the non-control characters because you are mirroring what they type onto the screen.

Off the top of my head, I can’t think of a way to filter those out. There is a keyPressed command in javascript, but I don’t believe it was implemented into GameLab. GameLab is mostly designed as a training tool for beginning computer science students and not intended for more advanced projects. It can work for advanced projects, but it’s easy to get past the practical limits of the environment if you get too complex.

I’ll see if I can ping some of the users who have made some more advanced projects. They may have some ideas for you.

Mike

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 13, 2021, 9:18pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/15 "2021-12-13T21:18:37Z")

</div>

Thanks. Yeah I was hoping something like keyPressed() would have been implemented. Another issue I’m having is that it uses the intl keyboard rather than the one I was hoping (Canadian keyboard), but I don’t think that I can fix that.

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [December 16, 2021, 10:05pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/16 "2021-12-16T22:05:27Z")

</div>

There’s a functions for it in P5 that you can use in gameLab.  
keyPressed()  
keyReleased()  
keyTyped()  
However from the disscussion I’ve seen that you want only non control keys to be checked for.  
There’s keyTyped for that, and it will be activated when the user types a key  
The usage is the same as keyPressed.  
The keyTyped function is called once every time a key is pressed, but action keys such as Backspace, Delete, Ctrl, Shift, and Alt are ignored.  
Please note that if you try to use the “key” variable in order to get what key was pressed, it will literally output whatever you typed, meaning your program will have to accept keys that are in capital, but that’s not important. So yes, don’t use keyPressed, use keyTyped instead.

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [December 16, 2021, 10:08pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/17 "2021-12-16T22:08:21Z")

</div>

> [@mwood](#):
>
> Off the top of my head, I can’t think of a way to filter those out. There is a keyPressed command in javascript, but I don’t believe it was implemented into GameLab.

KeyPressed() is a function and it actually works in GameLab as it is in P5, but for their situation keyTyped is better (since they don’t want control keys, if I’m understood everything correctly…)

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 22, 2021, 6:05am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/18 "2021-12-22T06:05:08Z")

</div>

Sorry I’m 5 days late to the reply, I got sick and had some other things to deal with. Gamelab uses P5 js?

---

<div class="post-metadata">

**Author:** ![eclecticmssc](https://avatars.discourse-cdn.com/v4/letter/e/e480ec/32.png) [@eclecticmssc](https://forum.code.org/u/eclecticmssc)\
**Post date:** [December 22, 2021, 6:08am UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/19 "2021-12-22T06:08:06Z")

</div>

![Screenshot 2021-12-21 23.07.47](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/1/1730d8a5957fce1a0d4ac7b317d2fc3498ca208a.png)  
Also this

Edit: I have found that I need to explicityly declare these, thanks for the help.

---

<div class="post-metadata">

**Author:** ![IMPixel](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/impixel/32/6744_2.png) [@IMPixel](https://forum.code.org/u/IMPixel)\
**Post date:** [December 22, 2021, 12:31pm UTC](https://forum.code.org/t/is-there-any-way-to-tell-if-any-key-is-pressed/35572/20 "2021-12-22T12:31:35Z")

</div>

Oh, great. Yeah, GameLab uses p5.js, a lot of functions that are from p5.js are in gameLab. Also consider using KeyTyped instead of keyPressed in order to ignore control keys. KeyPressed works weird in some situation and will sometimes not give the exact key the user is typing.
