Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript disable space scrolling

How do I disable space scrolling? I am making a canvas game (like agar.io) and I don't want the user to scroll down when he presses the space key, but I still want the canvas to recognize it as the user pressing space down. I am using p5.js as the canvas library.

like image 553
Big Ben GamerGuyKSPMC Avatar asked Sep 27 '16 17:09

Big Ben GamerGuyKSPMC


Video Answer


1 Answers

This is covered in the reference:

Browsers may have different default behaviors attached to various key events. To prevent any default behavior for this event, add "return false" to the end of the method.

In other words, you can simply return false from the keyPressed() function:

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

function draw() {

}

function keyPressed(){
  text("here", random(width), random(height));
  return false;
}

This indicates that the page should not execute any default behavior. So you might only want to return false in the case of certain keys.

You also might want to add similar return false statements in the other mouse event functions to avoid the case where the user holds down the space key.

like image 147
Kevin Workman Avatar answered Oct 06 '22 18:10

Kevin Workman