Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 308
Vlad Avatar asked Oct 23 '25 15:10

Vlad


1 Answers

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;
}
like image 76
Dhara Parmar Avatar answered Oct 26 '25 04:10

Dhara Parmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!