任何方式来防止/禁用浏览器中的CTRL + [key]快捷键? [英] Any way to prevent/disable CTRL + [key] shortcuts in the browser?

查看:232
本文介绍了任何方式来防止/禁用浏览器中的CTRL + [key]快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多人会对这个被问到的问题感到愤怒,但是...

我有一个使用WebGL和Pointer Lock API的游戏。由于很多游戏在CTRL上有蹲的性质,我想知道是否有任何可能的方法来停止像CTRL + S和CTRL + W这样的浏览器快捷方式。



目前我不得不严格禁止控件在其中有任何CTRL键。我为C设置了'蹲',这也很常见,但我也有关于制作MMORPG风格游戏的想法,在这种游戏中,您将拥有多个操作栏,并且由于CTRL不可行,因此无法进行多种组合。注意:在Chrome中 Ctrl + W / kbd>是保留,请使用 window.onbeforeunload


试试这个(禁用 Ctrl + W Ctrl + S ):

  window.onbeforeunload = function(){//防止Ctrl + W 
返回真的想退出游戏吗?;
};

document.onkeydown = function(e){
e = e || window.event; //获取事件
if(e.ctrlKey){
var c = e.which || e.keyCode; //获取密钥代码
switch(c){
case 83:// Block Ctrl + S
case 87:// Block Ctrl + W - Chrome中无法使用
e.preventDefault();
e.stopPropagation();
休息;
}
}
};


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.

解决方案

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

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

window.onbeforeunload = function () {//Prevent Ctrl+W
    return "Really want to quit the game?";
};

document.onkeydown = function (e) {
    e = e || window.event;//Get event
    if (e.ctrlKey) {
        var c = e.which || e.keyCode;//Get key code
        switch (c) {
            case 83://Block Ctrl+S
            case 87://Block Ctrl+W --Not work in Chrome
                e.preventDefault();     
                e.stopPropagation();
            break;
        }
    }
};

这篇关于任何方式来防止/禁用浏览器中的CTRL + [key]快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆