Vlad
Vlad

Reputation: 2773

How to disable keyboard shortcuts completely from JavaScript

I am using Phaser 2.2.2 to develop a typing game and there is method called:

game.input.keyboard.addKeyCapture(/*[array of keycodes]*/);

This worked with Space(no page scrolling), dot, comma dash(dash with a caused one webpage backward) but didn't work with single quote as I can still open Quick Find with it i.e the browser still detects the shortcut. This method prevents the bubbling state of the key when pressed to propagate to the browser, i.e the browser does not detect a keypress.

addKeyCapture

I also found this beautiful typing application which disables completely keyboard shortcuts, like magic: https://typing.io/ No additional configuration or installing addons or plugins.

How can I achieve this in my Phaser game as well? Should I put some third party JS libraries.

Ah and btw is this tool for the job: https://github.com/jeresig/jquery.hotkeys

Upvotes: 4

Views: 17059

Answers (3)

asciicoding
asciicoding

Reputation: 9

Short answer:

window.onkeydown = function(key){if(key.ctrlKey == true){key.preventDefault()}};

Upvotes: 0

Bob
Bob

Reputation: 166

This is too late, but if you want to block keyboard shortcuts, most shortcuts use ctrl key, and the code to block that key using jQuery would be:

$("input[name='yourinput']").keypress(function(event) {
    if ( event.keyCode == 17 ) {
         event.preventDefault();
    }
});

Note: key 17 is ctrl key.

Upvotes: 1

Dhara Parmar
Dhara Parmar

Reputation: 8101

The onkeydown event occurs when the user is pressing a key, you can prevent it using returing false:

Use:

document.onkeydown = function (e) {
        return false;
}

Upvotes: 7

Related Questions