Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我无法按下alt键?_Javascript_Events_Browser_Keyboard Shortcuts - Fatal编程技术网

Javascript 为什么我无法按下alt键?

Javascript 为什么我无法按下alt键?,javascript,events,browser,keyboard-shortcuts,Javascript,Events,Browser,Keyboard Shortcuts,我正在尝试使用Alt键将快捷方式设置为Shift+Alt+a或Ctrl+Alt+a,但当按下`Shift或Ctrl键时,我无法确定是否按下了Alt键 。下面的代码使问题更加清楚 el.onkeydown=function(e){ //alert(e.keyCode); if(e.shiftKey==true){ document.body.appendChild(document.createTextNode("shiftkey")); document

我正在尝试使用
Alt
键将快捷方式设置为
Shift+Alt+a
Ctrl+Alt+a
,但当按下
`Shift
Ctrl
键时,我无法确定是否按下了
Alt
键 。下面的代码使问题更加清楚

    el.onkeydown=function(e){
    //alert(e.keyCode);

    if(e.shiftKey==true){
    document.body.appendChild(document.createTextNode("shiftkey"));
    document.body.appendChild(document.createElement("br"));
    }
    else if(e.ctrlKey==true){
    document.body.appendChild(document.createTextNode("ctrlkey"));
    document.body.appendChild(document.createElement("br"));
    }
    else if(e.altKey==true){
    document.body.appendChild(document.createTextNode("altkey"));
    document.body.appendChild(document.createElement("br"));
    }
    };
当我尝试在按住Shift键或Ctrl键后按Alt键时,
e.altKey不能得到true值,我得到的结果如下

    shiftkey
    shiftkey
    shiftkey
    shiftkey
    shiftkey
    shiftkey...
或对于
Ctrl

    ctrlkey
    ctrlkey
    ctrlkey
    ctrlkey
    ctrlkey
    ctrlkey
    ctrlkey...

设计缺陷。您正在检查是否按下了换档键
。如果不是,那么你检查其他的。让我们一步一步地想一想

你先按shift键。事件激发,并且满足您的第一个条件。现在,当按下alt键时,将再次调用
keydown
事件,这一次,由于同时按下了
shift
alt
,因此满足了第一个和第三个条件。但是,程序永远不会达到第三个条件,因为它在else子句中。这意味着只计算第一个条件的代码,而跳过其余条件,因为第一个条件为true

更改设计以单独检查所有键,而不是使用
else
子句检查每个键

您的代码如下所示:

el.onkeydown=function(e){
//alert(e.keyCode);

if(e.shiftKey==true){
document.body.appendChild(document.createTextNode("shiftkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.ctrlKey==true){
document.body.appendChild(document.createTextNode("ctrlkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.altKey==true){
document.body.appendChild(document.createTextNode("altkey"));
document.body.appendChild(document.createElement("br"));
}
};
if(e.shiftKey && e.altKey && e.keyCode === 65){
    console.log('Shortcut active');
}
想想这个例子,让你的缺陷更加清晰:

if(true){
    console.log('First');
}
else if(true){
    console.log('Second');
}
您可以看到,第二个条件是否为真并不重要,只要第一个条件为真

不过,在您的情况下,使用
&&
逻辑运算符更有意义,因为您只想在按住所有三个键以创建快捷方式时执行某些操作。这将使您的代码如下所示:

el.onkeydown=function(e){
//alert(e.keyCode);

if(e.shiftKey==true){
document.body.appendChild(document.createTextNode("shiftkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.ctrlKey==true){
document.body.appendChild(document.createTextNode("ctrlkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.altKey==true){
document.body.appendChild(document.createTextNode("altkey"));
document.body.appendChild(document.createElement("br"));
}
};
if(e.shiftKey && e.altKey && e.keyCode === 65){
    console.log('Shortcut active');
}

设计缺陷。您正在检查是否按下了换档键
。如果不是,那么你检查其他的。让我们一步一步地想一想

你先按shift键。事件激发,并且满足您的第一个条件。现在,当按下alt键时,将再次调用
keydown
事件,这一次,由于同时按下了
shift
alt
,因此满足了第一个和第三个条件。但是,程序永远不会达到第三个条件,因为它在else子句中。这意味着只计算第一个条件的代码,而跳过其余条件,因为第一个条件为true

更改设计以单独检查所有键,而不是使用
else
子句检查每个键

您的代码如下所示:

el.onkeydown=function(e){
//alert(e.keyCode);

if(e.shiftKey==true){
document.body.appendChild(document.createTextNode("shiftkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.ctrlKey==true){
document.body.appendChild(document.createTextNode("ctrlkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.altKey==true){
document.body.appendChild(document.createTextNode("altkey"));
document.body.appendChild(document.createElement("br"));
}
};
if(e.shiftKey && e.altKey && e.keyCode === 65){
    console.log('Shortcut active');
}
想想这个例子,让你的缺陷更加清晰:

if(true){
    console.log('First');
}
else if(true){
    console.log('Second');
}
您可以看到,第二个条件是否为真并不重要,只要第一个条件为真

不过,在您的情况下,使用
&&
逻辑运算符更有意义,因为您只想在按住所有三个键以创建快捷方式时执行某些操作。这将使您的代码如下所示:

el.onkeydown=function(e){
//alert(e.keyCode);

if(e.shiftKey==true){
document.body.appendChild(document.createTextNode("shiftkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.ctrlKey==true){
document.body.appendChild(document.createTextNode("ctrlkey"));
document.body.appendChild(document.createElement("br"));
}
if(e.altKey==true){
document.body.appendChild(document.createTextNode("altkey"));
document.body.appendChild(document.createElement("br"));
}
};
if(e.shiftKey && e.altKey && e.keyCode === 65){
    console.log('Shortcut active');
}