本文介绍了使用JavaScript在Web应用程序中冻结键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to freeze keyboard in a web application? i had tried freezing shortcuts,but it is only showing alert,which is defined in the freezing function. But the freeze operation is not working

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;

}

推荐答案

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

cancel the keydown event to prevent commands from going through

document.addEventListener("keydown", function(e){e.preventDefault();return false;}, false);

这篇关于使用JavaScript在Web应用程序中冻结键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:27