Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to prevent/disable CTRL + [key] shortcuts in the browser?

I know a lot of people will be angry about this being asked but...

I have a game using WebGL and the Pointer Lock API. Due to the nature of a lot of games having 'crouch' on CTRL I was wondering if there was any possible way to stop browser shortcuts like CTRL + S and CTRL + W...

At the moment I'm having to harshly disallow controls to have any CTRL key in them. I have set 'crouch' to C which is also common but I also have ideas about making a MMORPG-style game where you would have several action bars of abilities and a lot of combinations wouldn't be possible due to CTRL not being viable.

like image 733
user2000236 Avatar asked Jul 15 '14 17:07

user2000236


People also ask

How do I turn off CTRL shortcuts?

Click on “File Explorer.” Double click on “Turn off Windows Key Hotkeys” in the right-hand pane. This should launch a popup window where you can turn off keyboard shortcuts. Click on “Enabled,” select “Apply,” and then click on “Ok” to save your new settings.

How do I disable CTRL Shift on my website?

ctrlKey && event. shiftKey && event. keyCode==73) { alert('Entered ctrl+shift+i') return false; //Prevent from ctrl+shift+i } else if(event. ctrlKey && event.


1 Answers

Note: In Chrome Ctrl+W is "reserved", use window.onbeforeunload

Note: Chrome requires event.returnValue to be set

In this code document.onkeydown is used for old browsers and window.onbeforeunload is used to Chrome and Firefox

Try this (disable Ctrl+W and Ctrl+S):

window.onbeforeunload = function (e) {
    // Cancel the event
    e.preventDefault();

    // Chrome requires returnValue to be set
    e.returnValue = 'Really want to quit the game?';
};

//Prevent Ctrl+S (and Ctrl+W for old browsers and Edge)
document.onkeydown = function (e) {
    e = e || window.event;//Get event

    if (!e.ctrlKey) return;

    var code = e.which || e.keyCode;//Get key code

    switch (code) {
        case 83://Block Ctrl+S
        case 87://Block Ctrl+W -- Not work in Chrome and new Firefox
            e.preventDefault();
            e.stopPropagation();
            break;
    }
};
like image 57
Guilherme Nascimento Avatar answered Sep 17 '22 00:09

Guilherme Nascimento