Arrow keys Key_Pressed overpowering other keys?

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!

  • edited March 2017

    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

  • edited March 2017

    @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

  • edited March 2017

    @Polyrogue --

    if I hold down up+down

    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.

    // arrow key game control test
    // Jeremy Douglass -- 2017-03-16 -- p5.js 1.0.3
    
    var lcolor, rcolor, ucolor, dcolor;
    
    function setup() {
      createCanvas(400, 400);
    }
    
    function draw() {
      background(128);
    
      lcolor = (keyIsDown(LEFT_ARROW)  ? color(255,  64, 255) : color(128));
      rcolor = (keyIsDown(RIGHT_ARROW) ? color(255,  64,  64) : color(128));
      ucolor = (keyIsDown(UP_ARROW)    ? color( 64, 255,  64) : color(128));
      dcolor = (keyIsDown(DOWN_ARROW)  ? color( 64,  64, 255) : color(128));  
    
      lkey(0,height/3, width/3, height/3, 20);
      rkey(2*(width/3), height/3, width/3, height/3, 20);
      ukey(width/3, 0, width/3, height/3, 20);
      dkey(width/3, 2*(height/3), width/3, height/3, 20); 
    }
    
    function lkey(x,y,w,h,margin){
      translate(x,y);
      keyplate(w,h);
      stroke(lcolor);
      strokeWeight(10);
      rect(margin, margin, w - 2*margin, h - 2*margin);
      translate(-x,-y);
    }
    function rkey(x,y,w,h,margin){
      translate(x,y);
      keyplate(w,h);
      stroke(rcolor);
      strokeWeight(10);
      ellipse(w/2, h/2, w - 1.5*margin, h - 1.5*margin);
      translate(-x,-y);
    }
    function ukey(x,y,w,h,margin){
      translate(x,y);
      keyplate(w,h);
      stroke(ucolor);
      strokeWeight(10);
      triangle(w/2, margin, w - margin, h - margin, margin, h - margin);
      translate(-x,-y);
    }
    function dkey(x,y,w,h,margin){
      translate(x,y);
      keyplate(w,h);
      stroke(dcolor);
      strokeWeight(10);
      line(margin,margin, w - margin, h - margin);
      line(w - margin, margin, margin, h - margin);
      translate(-x,-y);
    }
    function keyplate(w,h){
      fill(64);
      strokeWeight(0);
      rect(0, 0, w, h)
    }
    
Sign In or Register to comment.