html - Javascript disable space scrolling - Stack Overflow

How do I disable space scrolling?I am making a canvas game (like agar.io) and I don't want the u

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.

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.

Share Improve this question asked Sep 27, 2016 at 17:30 Big Ben GamerGuyKSPMCBig Ben GamerGuyKSPMC 1492 silver badges13 bronze badges 4
  • Possible duplicate of Pressing spacebar moves page down? – Liran H Commented Sep 27, 2016 at 17:36
  • 3 @LiranH Please note that this is a p5.js question, which contains its own key event functions. The answer you linked is on the right track, but it doesn't really help in this specific case. – Kevin Workman Commented Sep 27, 2016 at 17:40
  • Right you are, my apologies for flagging too quickly without more thought. – Liran H Commented Sep 30, 2016 at 13:56
  • @Liran Well, now I am banned thanks to you... – Big Ben GamerGuyKSPMC Commented Sep 30, 2016 at 20:47
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633653a4637256.html

相关推荐

  • html - Javascript disable space scrolling - Stack Overflow

    How do I disable space scrolling?I am making a canvas game (like agar.io) and I don't want the u

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信