We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there, so I have some fairly simple code here for a player spaceship:
function keyPressed()
{
if(key === " ")
{
bullets.push(new Bullet(player.x+((player.w)*player.dir),player.y+(player.h-3)));
}
if(keyCode == LEFT_ARROW)
{
moveLeft = true;
}
if(keyCode == RIGHT_ARROW)
{
moveRight = true;
}
if(keyCode == UP_ARROW)
{
moveUp = true;
}
if(keyCode == DOWN_ARROW)
{
moveDown = true;
}
return false;
}
And normally this works perfectly, except that if I hold down up+down, down+right, down+left, or up+left and pressing space does nothing and I am not sure why. Stranger still, holding down left+right or up+right still allows pressing space to work.
I'm not sure what's going on here. Any help would be greatly appreciated.
Thanks!
EDIT: I have an example right here: http://polyrogue.com/defender/index.html
Answers
Once you use key, once keyCode
to register only one key use else if through out
To register multiple keys search multiple keys, comment by gotoloop iirc
Hi there, thanks for the reply.
I can't really tell if either of those is the solution to what I want done, but I apologise if it's me misinterpreting your answer. I don't need multiple keys to be pressed for a specific event to fire, I need all my individual events caused by individual key presses to fire independently, so that it shouldn't matter what other keys are currently being held down, the other keys should still work. Is that the same as what you said?
Thanks!
I had some problems with multiple key pressing interfering with each other so I used keyIsDown instead and that seemed to keep them all independent. Not sure if this is what you need but it might help.
https://p5js.org/reference/#/p5/keyIsDown
@Chrisir Can you please post a link to the comment by GoToLoop you're talking about, I can't seem to find it. Are you talking about this- https://forum.processing.org/two/discussion/16594/#Comment_68096
https://forum.processing.org/two/discussion/9582/how-to-have-simultaneous-key-presses
@Polyrogue --
This may be a hardware limitation of your specific keyboard -- it is very common for certain key combinations to be unpress-able. For example, my laptop keyboard does support up+down, left+right, and up+left, but it does not support pressing any three arrow keys at the same time -- so up+left+right will not work.
If this is a hardware limitation then there is nothing Processing can do, as the keyboard isn't sending signals to the computer. You can test this with a keyboard inspector outside Processing. Here are some discussions of similar issues:
For fun, here is a test sketch that uses keyIsDown. If it is a hardware issue that won't really fix the issue compared to keyPressed as @pyan83 suggests.