Keyboard overlaps with web browser commands(html5)

8bitDev

  • Posts: 23
When using keyboard on html5 game(once uploaded to newgrounds),if i use arrow keys web browser window goes up and down and when using space bar it goes instantly down the page while at the same time game input is detected in the game.

How can i limit commands only to the web game that i have compared to the web browser commands?

I used build 9180 and just wandering before upgrading to the newest release if i am doing something wrong or if there is a fix to this issue/workaround in another end?

Regards

letmethink

  • *
  • Posts: 2545
Put this in a code block in the first scene of your game in a when created event:

Code: [Select]
untyped __js__('var keys = {};
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = true;
        switch(e.keyCode){
            case 37: case 39: case 38:  case 40: // Arrow keys
            case 32: e.preventDefault(); break; // Space
            default: break; // do not block other keys
        }
    },
false);
window.addEventListener("keyup",
    function(e){
        keys[e.keyCode] = false;
    },
false);');
~Letmethink

orbinho11

  • Posts: 13
Put this in a code block in the first scene of your game in a when created event:

Code: [Select]
untyped __js__('var keys = {};
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = true;
        switch(e.keyCode){
            case 37: case 39: case 38:  case 40: // Arrow keys
            case 32: e.preventDefault(); break; // Space
            default: break; // do not block other keys
        }
    },
false);
window.addEventListener("keyup",
    function(e){
        keys[e.keyCode] = false;
    },
false);');

Thank you very very much, worked perfectly =)

8bitDev

  • Posts: 23
Thank you,it worked in all of my games without any issue.

Put this in a code block in the first scene of your game in a when created event:

Code: [Select]
untyped __js__('var keys = {};
window.addEventListener("keydown",
    function(e){
        keys[e.keyCode] = true;
        switch(e.keyCode){
            case 37: case 39: case 38:  case 40: // Arrow keys
            case 32: e.preventDefault(); break; // Space
            default: break; // do not block other keys
        }
    },
false);
window.addEventListener("keyup",
    function(e){
        keys[e.keyCode] = false;
    },
false);');