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.
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.
ctrlKey && event. shiftKey && event. keyCode==73) { alert('Entered ctrl+shift+i') return false; //Prevent from ctrl+shift+i } else if(event. ctrlKey && event.
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;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With