1

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?

2 Answers 2

1

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?.

Sign up to request clarification or add additional context in comments.

1 Comment

Thats very interesting. Thanks!
0

The way keyboards are wired prevents them from detecting multiple keys in certain combinations, so it will ignore the extra keys in what's known as N-key rollover. It's not p5.js at all.

You can look at this link:

https://www.quora.com/What-is-anti-ghosting-for-a-keyboard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.