我知道很多人会因为被问到这个问题而生气,但是......
我有一个使用 WebGL 和 Pointer Lock API 的游戏。由于很多游戏在 CTRL 上都有“蹲伏”的性质,我想知道是否有任何可能的方法来停止浏览器快捷方式,如 CTRL + S 和 CTRL + W...
目前我不得不严厉地禁止控件中有任何 CTRL 键。我已将“蹲伏”设置为 C,这也很常见,但我也有制作 MMORPG 风格游戏的想法,在这种游戏中,您将拥有多个能力 Action 条,并且由于 CTRL 不可行,许多组合是不可能的。
最佳答案
Note: In Chrome Ctrl+W is "reserved", use
window.onbeforeunload
Note: Chrome requires
event.returnValue
to be set
在此代码中,document.onkeydown
用于旧浏览器,window.onbeforeunload
用于 Chrome 和 Firefox
试试这个(禁用 Ctrl+W 和 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;
}
};
关于javascript - 有什么方法可以防止/禁用浏览器中的 CTRL + [key] 快捷方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764626/