Search code examples
javascriptkeyp5.jskeycode

P5.js SOME Keycodes not responding


I've been surfing through a lot of old P5.js questions and ran into this question: p5.js code isnt executing when spacebar is pressed in combiniation with up and left I tried out a few of my ideas in the editor (which didn't work), then I made this little test program:

function setup(){
  
  createCanvas(200, 200);
  
}

function draw() {
  
  background(220);
  
  textAlign(CENTER, CENTER);
  text( keyCode, width/2, height/2 );
  
  let lettersPressed = "";
  
  let toKeyCode = k => {
    
    return 65 + "abcdefghijklmnopqrstuvwxyz".split("").indexOf(k);
    
  }
  
  "abcdefghijklmnopqrstuvwxyz".split("").forEach(l => {
      
    lettersPressed += keyIsDown(toKeyCode(l)) ? l : "-"; 
    
  });
  
  text(lettersPressed, width/2, height/1.7);
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.min.js"></script>

The first number is the keycode of the most recent key press. The letters/dashes after that is all the letters in the alphabet that are being pressed.

THE QUESTION

When I press down the a, b, and c key they all show up. But when I press the d key on top of that, the d key doesn't show. This is a very similar problem to the SO I linked. The weird part is you can press the e key and it works. This is a pattern throughout other combinations of keys as well. Is their a reason for this?


Solution

  • This has nothing to do with p5.js, it has everything to do with the physical keyboard you're using and your computer/operating system/drivers.

    Keyboards can only transfer so much data, so they limit the number of keys that they report pressed at any given time. Sometimes they do this to X number of keys per area.

    Watch Ben Eater's How does a USB keyboard work? for more details, as well as How does n-key rollover work?.