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.?
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
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.
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:
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
Why you don’t want to make an array?
Except, wouldn’t that be true if no key at all was pressed?
Mike
Can you add a second condition? (!keyDown(“ñ”)) && (!keyDown(“ ”))?
Can you share your app and explain what is it that you are trying to accomplish?
Making a sort of terminal for a game to get user input.
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())
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
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.
Look for the “Share” link in the upper left hand corner of the screen and get the “shared” link.
Mike
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
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
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.
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.
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…)
Sorry I’m 5 days late to the reply, I got sick and had some other things to deal with. Gamelab uses P5 js?
Also this
Edit: I have found that I need to explicityly declare these, thanks for the help.
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.