1

如何在 Web 应用程序中冻结键盘?我曾尝试冻结快捷方式,但它只显示警报,这是在冻结功能中定义的。但是冻结操作不起作用

function disableCtrlKeyCombination(e)
{
    //list all CTRL + key combinations you want to disable
    var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'v', 'j');
    var key;
    var isCtrl;

    if(window.event)
    {
            key = window.event.keyCode;     //IE
            if(window.event.ctrlKey)
                    isCtrl = true;
            else
                    isCtrl = false;
    }

    else
    {
            key = e.which;     //firefox
            if(e.ctrlKey)
                    isCtrl = true;
            else
                    isCtrl = false;
    }

    //if ctrl is pressed check if other key is in forbidenKeys array
    if(isCtrl)
    {
        for(i = 0 ; i < forbiddenKeys.length ; i++)
            {

                    //case-insensitive comparation
                    if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
                    {
                             alert('Key combination CTRL + '
                                    +String.fromCharCode(key)
                                    +' has been disabled.'); 
                            return false;
                    }
            }
    }       
    return true;

}

4

1 回答 1

1

取消 keydown 事件以防止命令通过

document.addEventListener("keydown", function(e){e.preventDefault();return false;}, false);
于 2012-11-07T12:39:32.927 回答