检测组合按键(Control,Alt,Shift)? [英] Detecting combination keypresses (Control, Alt, Shift)?

查看:197
本文介绍了检测组合按键(Control,Alt,Shift)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按下 Ctrl + Alt + e 时运行脚本。

怎么能Tampermonkey同时按下ctrl,alt和e键?



我试过 ctrlKey 方altKey 。我发现没有任何效果。

我如何编辑下面的脚本来触发 Ctrl + Alt + e ,而不仅仅是 e

 (function(){
document.addEventListener (keypress,function(e){
if(e.which == 101){
var xhttp = new XMLHttpRequest; xhttp.onreadystatechange = function(){4 == xhttp.readyState&& ; 200 == xhttp.status&安培;&安培;的eval(xhttp.responseText)},xhttp.open( GET, http://127.0.0.1:2337/inject,0!),xhttp.send();
}
});
})();


解决方案

参考键盘事件的W3C规范。提供了几个布尔属性来确定修饰键是否与您感兴趣的目标键一起被按下。它们是:




  • ctrlKey     - 还按下了控制键。

  • shiftKey   - 还按下了Shift键。

  • altKey       - 同时按下Alt键。

  • metaKey     - Meta键也被按下了。



其他重要说明


  1. 属性已被弃用

  2. 使用 keydown 因为 Chrome无法启动 keypress 已知键盘快捷键的事件。

  3. 某些规格属性,例如仅在Firefox中部分正常运行

  4. 您不需要将代码包装在像Tampermonkey(或Greasemonkey或大多数用户脚本引擎)那样的匿名函数中。自动提供范围保护。

因此,您的代码将变为:

  document.addEventListener(keydown,function(zEvent){
if(zEvent.ctrlKey&& zEvent.altKey&& zEvent.code ===) KeyE){
//在这里做你的礼物
}
});






运行这个方便的演示:



< pre class =snippet-code-js lang-js prettyprint-override> var targArea = document.getElementById(keyPrssInp); targArea.addEventListener('keydown',reportKeyEvent); function reportKeyEvent(zEvent ){var reportStr =The+(zEvent.ctrlKey?Control:)+(zEvent.shiftKey?Shift:)+(zEvent.altKey?Alt:)+(zEvent .metaKey?Meta:)+ zEvent.code ++键被按下。 ; $(#statusReport)。text(reportStr); // ---是否按下了Ctrl-Alt-E组合? if(zEvent.ctrlKey&& zEvent.altKey&& zEvent.code ===KeyE){this.hitCnt =(this.hitCnt || 0)+ 1; $(#statusReport)。after('< p> Bingo!cnt:'+ this.hitCnt +'< / p>'); } zEvent.stopPropagation(); zEvent.preventDefault()}

 < script src = https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js\"></script><p><label>在这里按键:<输入类型=textvalue =id =keyPrssInp>< / label>< / p>< p id =statusReport>< / p>  


I am trying to make a script run when Ctrl + Alt + e is pressed.
How can Tampermonkey fire on a simultaneous ctrl, alt, and e key?

I have tried ctrlKey, and altKey. I've found nothing that works.
How can I edit the script below to fire on Ctrl + Alt + e, instead of just e?

(function() {
    document.addEventListener("keypress", function(e) {
    if (e.which == 101) {
        var xhttp=new XMLHttpRequest;xhttp.onreadystatechange=function(){4==xhttp.readyState&&200==xhttp.status&&eval(xhttp.responseText)},xhttp.open("GET","http://127.0.0.1:2337/inject",!0),xhttp.send();
    }
  });
})();

解决方案

Refer to the W3C spec for keyboard events. Several boolean attributes are provided to determine if modifier keys were pressed in conjunction with whatever target key you are interested in. They are:

  • ctrlKey     -- The "Control" key was also pressed.
  • shiftKey   -- The "Shift" key was also pressed.
  • altKey       -- The "Alt" key was also pressed.
  • metaKey     -- The "Meta" key was also pressed.

Other important notes:

  1. The which property is deprecated.
  2. Use keydown because Chrome does not fire the keypress event for known keyboard shortcuts.
  3. Some spec'd properties, such as key, are only partly functional in Firefox.
  4. You do not need to wrap your code in an anonymous function like that for Tampermonkey (or Greasemonkey or most userscript engines). Scope protection is automatically provided.

So, your code would become:

document.addEventListener ("keydown", function (zEvent) {
    if (zEvent.ctrlKey  &&  zEvent.altKey  &&  zEvent.code === "KeyE") {
        // DO YOUR STUFF HERE
    }
} );


Run this handy demo:

var targArea = document.getElementById ("keyPrssInp");
targArea.addEventListener ('keydown',  reportKeyEvent);

function reportKeyEvent (zEvent) {
    var reportStr   =
        "The " +
        ( zEvent.ctrlKey  ? "Control " : "" ) +
        ( zEvent.shiftKey ? "Shift "   : "" ) +
        ( zEvent.altKey   ? "Alt "     : "" ) +
        ( zEvent.metaKey  ? "Meta "    : "" ) +
        zEvent.code + " " +
        "key was pressed."
    ;
    $("#statusReport").text (reportStr);

    //--- Was a Ctrl-Alt-E combo pressed?
    if (zEvent.ctrlKey  &&  zEvent.altKey  &&  zEvent.code === "KeyE") {
        this.hitCnt = ( this.hitCnt || 0 ) + 1;
        $("#statusReport").after (
            '<p>Bingo! cnt: ' + this.hitCnt + '</p>'
        );
    }
    zEvent.stopPropagation ();
    zEvent.preventDefault ()
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p><label>Press keys in here:<input type="text" value="" id="keyPrssInp"></label>
</p>
<p id="statusReport"></p>

这篇关于检测组合按键(Control,Alt,Shift)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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