[英]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 :其他重要说明:
which
property is deprecated . 不推荐使用which
属性。keydown
becauseChrome does not fire the keypress
event for known keyboard shortcuts.使用keydown
因为Chrome 不会触发已知键盘快捷键的keypress
事件。key
, are only partly functional in Firefox .一些规范的属性,例如key
,在 Firefox 中只是部分功能。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.相反,您可以同时使用keydown
和keyup
事件来检测多个按键。 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 的答案对于您将修饰键与单个键代码目标结合使用是一个更好的解决方案,因为ctrlKey
和altKey
修饰符是组合检测的,而不是独立处理的。 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.例如,如果您想同时检测多个键代码,例如E
和F
,您需要按照上面的方法使用keydown
和keyup
来跟踪它们。
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。
Ctrl+Alt+E<\/code> (as in OP question) gives out €<\/code> .
所以Ctrl+Alt+E<\/code> (如在 OP 问题中)给出€<\/code> 。
Same it's true for Ctr+Alt+5<\/code> .
Ctr+Alt+5<\/code>也是如此。
Other combinations have the same problem, like Ctrl+Alt+ò<\/code> which on Italian keyboards gives out @<\/code>
其他组合也有同样的问题,比如Ctrl+Alt+ò<\/code>在意大利语键盘上给出@<\/code>
<\/li>
Every Shifted character is found in e.key<\/code> as it's shifted counterpart, keeping the modifier attributes, so Shift+5<\/code> prints out as Shift+%<\/code> which is questionable (given the AltGr behaviour).
每个 Shifted 字符都在e.key<\/code>中找到,因为它是移位对应物,保留修饰符属性,因此Shift+5<\/code>打印为Shift+%<\/code>这是有问题的(考虑到 AltGr 行为)。
<\/li>
Some combinations don't trigger the event no matter what.有些组合无论如何都不会触发事件。 I've found as examples Ctrl+Shift+9<\/code> , Ctrl+Shift+0<\/code> , Ctrl+Shift+L<\/code> and Ctrl+Shift+Alt+D<\/code> on my keyboard.
我在我的键盘上找到了Ctrl+Shift+9<\/code> 、 Ctrl+Shift+0<\/code> 、 Ctrl+Shift+L<\/code>和Ctrl+Shift+Alt+D<\/code>作为示例。
That might be a limitation of membrane keyboards.这可能是薄膜键盘的限制。<\/li><\/ul> The second point was critical for me, since our clients are used to Ctrl+Shift+[Number] combinations.第二点对我来说很关键,因为我们的客户习惯于 Ctrl+Shift+[Number] 组合。
I've solved it by handling the special case of keyCode being between 48 and 58 and converting manually to the corresponding digit.我通过处理 keyCode 介于 48 和 58 之间的特殊情况并手动转换为相应的数字来解决它。 It still doesn't solve any other special character modified with Shift though, but given that they are extremely depended on the keyboard layout, I don't see any universal solution.虽然它仍然没有解决任何其他用 Shift 修改的特殊字符,但是鉴于它们非常依赖于键盘布局,我看不到任何通用的解决方案。 Still, I've never known anyone that wanted a shortcut with special characters.尽管如此,我从来不知道有人想要一个带有特殊字符的快捷方式。
If readability is not a requirement and\/or you know in advance the key combination you want to check, using
event.keyCode<\/a> instead of
event.key<\/code> is probably safer.
如果不要求可读性和\/或您事先知道要检查的组合键,则使用
event.keyCode<\/a>而不是
event.key<\/code>可能更安全。
"
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.