简体   繁体   English

"检测组合按键(Control、Alt、Shift)?"

[英]Detecting combination keypresses (Control, Alt, Shift)?

I am trying to make a script run when Ctrl<\/kbd> + Alt<\/kbd> + e<\/kbd> is pressed.我试图在按下 Ctrl<\/kbd> + Alt<\/kbd> + e<\/kbd>时运行脚本。
How can Tampermonkey fire on a simultaneous ctrl, alt, and e key? Tampermonkey 如何同时使用 ctrl、alt 和 e 键触发?

I have tried ctrlKey<\/code> , and altKey<\/code> .我试过ctrlKey<\/code>和altKey<\/code> 。 I've found nothing that works.我没有发现任何有用的东西。
How can I edit the script below to fire on Ctrl<\/kbd> + Alt<\/kbd> + e<\/kbd> , instead of just e<\/kbd> ?如何编辑下面的脚本以在Ctrl<\/kbd> + Alt<\/kbd> + e<\/kbd>上触发,而不仅仅是e<\/kbd> ?

 (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(); } }); })();<\/code><\/pre>

"

Refer tothe W3C spec for keyboard events . 有关键盘事件,请参阅 W3C 规范 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. ctrlKey “控制”键也被按下。
  • shiftKey -- The "Shift" key was also pressed. shiftKey -- "Shift" 键也被按下。
  • altKey -- The "Alt" key was also pressed. altKey ——“Alt”键也被按下。
  • metaKey -- The "Meta" key was also pressed. metaKey ——“Meta”键也被按下。

Other important notes :其他重要说明

  1. The which property is deprecated . 不推荐使用which属性
  2. Use keydown becauseChrome does not fire the keypress event for known keyboard shortcuts.使用keydown因为Chrome 不会触发已知键盘快捷键的keypress事件。
  3. Some spec'd properties, such as key , are only partly functional in Firefox .一些规范的属性,例如key在 Firefox 中只是部分功能
  4. You do not need to wrap your code in an anonymous function like that for Tampermonkey (or Greasemonkey or most userscript engines).您不需要将代码包装在像 Tampermonkey(或 Greasemonkey 或大多数用户脚本引擎)那样的匿名函数中。 Scope protection is automatically provided.范围保护是自动提供的。

So, your code would become:因此,您的代码将变为:

document.addEventListener ("keydown", function (zEvent) {
    if (zEvent.ctrlKey  &&  zEvent.altKey  &&  zEvent.key === "e") {  // case sensitive
        // DO YOUR STUFF HERE
    }
} );

Run this handy demo (updated now that key has full support) :运行这个方便的演示(现在更新, key是完全支持)

 var targArea = document.getElementById ("keyPrssInp"); targArea.addEventListener ('keydown', reportKeyEvent); function reportKeyEvent (zEvent) { var keyStr = ["Control", "Shift", "Alt", "Meta"].includes(zEvent.key) ? "" : zEvent.key + " "; var reportStr = "The " + ( zEvent.ctrlKey ? "Control " : "" ) + ( zEvent.shiftKey ? "Shift " : "" ) + ( zEvent.altKey ? "Alt " : "" ) + ( zEvent.metaKey ? "Meta " : "" ) + keyStr + "key was pressed." ; $("#statusReport").text (reportStr); //--- Was a Ctrl-Alt-E combo pressed? if (zEvent.ctrlKey && zEvent.altKey && zEvent.key === "e") { // case sensitive 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>

The keypress event is fired as soon as a key has been pressed. keypress事件在按键被按下后立即触发。 If you are pressing multiple keys, it will fire the event for each press, so they are considered independent key presses.如果您按下多个键,每次按下都会触发事件,因此它们被视为独立的按键。

Instead, you can use both the keydown and keyup events to detect multiple key presses.相反,您可以同时使用keydownkeyup事件来检测多个按键。 You can have an object that contains the 3 keys and a boolean state.您可以拥有一个包含 3 个键和一个布尔状态的对象。 On the keydown event, if the current key matches a key in the object, you set the state to true for that key.keydown事件中,如果当前键与对象中的键匹配,则将该键的状态设置为true On the keyup event, you reset the state for the current key to false .keyup事件中,您将当前键的状态重置为false If all 3 states are true at the time of the last key press, then fire the event.如果在最后一次按键时所有 3 个状态都为true ,则触发该事件。

See this example that achieves this logic using jQuery.请参阅使用 jQuery 实现此逻辑的示例

Update Brock's answer is a better solution for you using modifier keys in combination with a single key code target, as the ctrlKey and altKey modifiers are detected in combination, not handled independently.更新Brock 的答案对于您将修饰键与单个键代码目标结合使用是一个更好的解决方案,因为ctrlKeyaltKey修饰符是组合检测的,而不是独立处理的。 If you want to detect multiple key codes like, E and F together, for example, you'd need to keep track of them using keydown and keyup as per above.例如,如果您想同时检测多个键代码,例如EF ,您需要按照上面的方法使用keydownkeyup来跟踪它们。

If you, like me, came here to find out how to detect the combination of the "Alt Gr" key with a letter key, then the properties like for example event.altKey cannot be used for this, since there is no event.AltGrKey property.如果您像我一样来到这里了解如何检测“Alt Gr”键与字母键的组合,那么例如 event.altKey 之类的属性不能用于此,因为没有 event.AltGrKey财产。

In that case you can use在这种情况下,您可以使用

event.getModifierState('AltGraph')

For more details see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState有关更多详细信息,请参阅https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState

I'm using hotkeys-js<\/a> to abstract the functionality, but still had to let the user choose his own custom shortcut (without knowing it in advance).我正在使用hotkeys-js<\/a>来抽象功能,但仍然必须让用户选择自己的自定义快捷方式(事先不知道)。 I then tried @Brock Adams's answer to get an equivalent format of hotkeys-js of the shortcut pressed in an input box.然后我尝试了@Brock Adams 的答案,以获得输入框中按下的快捷键的等效格式的 hotkeys-js。

Things I've found while experimenting (on an Italian QWERTY keyboard layout on Chrome 92):我在实验时发现的东西(在 Chrome 92 上的意大利 QWERTY 键盘布局上):

Quick note.速记。 If you're detecting shift, then you need to capitalize whatever letter you're checking.如果您检测到班次,那么您需要将要检查的任何字母大写。 Shift capitalizes the normally lowercase letter. Shift 将通常的小写字母大写。

if (zEvent.shiftKey && zEvent.key === "T")<\/code>

vs对比

if (zEvent.ctrlKey && zEvent.key === "t")<\/code>

if (zEvent.altKey && zEvent.key === "t")<\/code>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM