本文介绍了检测组合按键(Control,Alt,Shift)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试按下 + + 时运行脚本。 怎么能Tampermonkey同时按下ctrl,alt和e键? 我试过 ctrlKey 和方altKey 。我发现没有任何效果。 我如何编辑下面的脚本来触发 + + ? (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 &NBSP; - 还按下了控制键。 shiftKey - 还按下了Shift键。 altKey &NBSP; &NBSP; - 同时按下Alt键。 metaKey &NBSP; - Meta键也被按下了。 其他重要说明: 属性已被弃用。 使用 keydown 因为 Chrome无法启动 keypress 已知键盘快捷键的事件。 某些规格属性,例如键,仅在Firefox中部分正常运行。 您不需要将代码包装在像Tampermonkey(或Greasemonkey或大多数用户脚本引擎)那样的匿名函数中。自动提供范围保护。 因此,您的代码将变为: document.addEventListener(keydown,function(zEvent){ if(zEvent.ctrlKey&& zEvent.altKey&& zEvent.code ===) KeyE){ //在这里做你的礼物} }); 运行这个方便的演示: 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 + + 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 + + , instead of just ?(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:The which property is deprecated.Use keydown because Chrome does not fire the keypress event for known keyboard shortcuts.Some spec'd properties, such as key, are only partly functional in Firefox.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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 21:47