3

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

Vlad
  • 2,739
  • 7
  • 49
  • 100

3 Answers3

7

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;
}
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • Will I be able to type letter with this, for example ~, ', ., - , Space ? – Vlad May 06 '16 at 13:15
  • Eh, that is not what I am looking for. I need to disable keyboard shortcuts, i.e prevent scrolling when pressing Space, or prevent Quick Find when pressing single quote. – Vlad May 06 '16 at 13:17
  • But space and single quote will become completely unusable? Or I do work before returning false for a specific key? – Vlad May 06 '16 at 13:25
  • for shortcut keys only you need to check using keyCode...sing key pressing will be handles automatically – Dhara Parmar May 06 '16 at 13:40
  • But they will become unusable. Yes i know you want points but, cannot use it. – Vlad May 06 '16 at 16:40
  • @ Khara Parmar, will this be able to block [ctrl/command + key](https://stackoverflow.com/questions/24764626/any-way-to-prevent-disable-ctrl-key-shortcuts-in-the-browser/24764878)? – Green Mar 31 '21 at 20:06
1

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.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Bob
  • 146
  • 9
0

Short answer:

window.onkeydown = function(key){if(key.ctrlKey == true){key.preventDefault()}};
Rich
  • 6,470
  • 15
  • 32
  • 53