检测使用JavaScript AltGr键

问题描述 投票:6回答:3

我怎样才能澄清ALT + CTRL和ALTGR按键?

我在这里发现这个代码可能的解决方案,但它不工作:

if (event.ctrlKey && event.altKey) {

}

此代码是ALT +点击率和ALTGR也如此。

我的情况是这样的:对于ALT + CTRL + E(例如E,这是不管)我想一件事和ALTGR + E另一个,我该怎么办呢?

如果有人有一些想法,请告诉我。

javascript jquery keyboard
3个回答
2
投票

您可以检测被按下的键(右键或左键)在location对象event属性的值。如果位置的属性值是1(e.location=1)然后左键被按下。如果值是2,那么正确的键被按下。

在这里,我提供我的RightAlter+RightCtrl+<any_valid_key>代码

勾选此例

var isRightAltKey=false;
var isRightCtrlKey=false;
var validKeys=['a','s','d','f','g']; //keep empty array if no need to check key


document.addEventListener("keydown", function(e) {
  if(e.key=="Alt"){
       // when right Alter pressed make isRightAltKey true 
       isRightAltKey= (e.location==2); 
       
  }
  else if(e.key=="Control"){
      // when right Control pressed make isRightCtrlKey true, 
      //if you need any ctrl key pressed then simply set isRightCtrlKey= true;
       isRightCtrlKey= (e.location==2); 
  }

  // checking both right key is pressed already or not?
  var isRightKeys= isRightAltKey && isRightCtrlKey;
  
  // validate other keys [optional]
  var isValidKey=((typeof validKeys === "undefined") || validKeys.length==0 || validKeys.indexOf(e.key.toLowerCase())>=0);
  
  if (isRightKeys && isValidKey){
    document.getElementById("detect_key").innerHTML = "RightAlt + RightCtrl + "+e.key; 
  }
  else
  {
    document.getElementById("detect_key").innerHTML="";
  }
}, false);

document.addEventListener("keyup", function(e) {
  if(e.key=="Alt"){
       // when right Alter released make isRightAltKey false
       isRightAltKey= false; 
       
  }
  else if(e.key=="Control"){
       // when right Control released make isRightCtrlKey false
       isRightCtrlKey= false; 
  }
}, false);
<div id="detect_key"></div>

为什么要重视keyup事件监听器?

下面我们就来检测时,Ctrl和Alt键被按下键的位置(上keydown事件)。我们必须将其存储在标志变量,并使其实现。当按键被释放(在keyup事件)必须标记为假。否则,这些标志始终保持真实。下一步按键,它会永远正确


1
投票

您可以使用位置被按下的ALT决心。为了支持Alt + Ctrl,我们将保存已按下Alt的最后位置。

Location = 1 // Left
Location = 2 // Right 

然后,一旦两个Alt和Ctrl被按下,做你的事。在这个例子中,我们只写结果DIV的Alt键侧。您可以添加的“E”挤压的状态,以及:

if (e.ctrlKey && e.altKey && e.key == "e"){

Example

HTML

<div class="cont">
    Click Alt + Ctrl<br /><br />
    <div id="res"></div>
</div>

使用Javascript

var lastAltLocation;

document.addEventListener("keydown", function(e) {
   if (e.key == "Alt"){
     lastAltLocation = e.location;
   }
   if (e.ctrlKey && e.altKey){
       if (lastAltLocation == 1){
           document.getElementById("res").innerHTML = "Left";
       }
       if (lastAltLocation == 2){
           document.getElementById("res").innerHTML = "Right";
       }
   } 
}, false);

0
投票

严格坚持在这里你的问题是两个要求的情况下,代码:

document.addEventListener ("keydown", function (zEvent) {
    if (zEvent.altKey  &&  zEvent.code === "KeyE") {
        if(zEvent.ctrlKey) {
            //Do Ctrl+Alt+E Stuff Here.
        } else {
            //Do Alt+E Stuff Here.
    }
});

现在,打破了这里的东西怎么回事。 qazxsw POI从而可以检测出多个按键。

首先,我们检查是否按Alt键和E键。如果是的话,我们再下去的Ctrl键,检查也很活跃,并根据需要采取适当的行动。

© www.soinside.com 2019 - 2024. All rights reserved.