e.metaKey 在 JavaScript MouseEvent 中指的是哪个键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6245868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which key does the e.metaKey refer to in JavaScript MouseEvent?
提问by Pacerier
MouseEvent.metaKey
doesn't seem to work. On both FireFox and Chrome, it returns false
even if I hold the Winkey while clicking:
MouseEvent.metaKey
似乎不起作用。在 FireFox 和 Chrome 上,false
即使我Win在单击时按住键,它也会返回:
<!doctype html>
<button onclick=alert(event.metaKey)>click while holding "meta key"</button>
MDN states:
MDN状态:
The
MouseEvent.metaKey
read-only property returning aBoolean
that indicates if the Metakey was pressed (true
) or not (false
) when the event occured.Note: On Macintosh keyboards, this is the command key (?). On Windows keyboards, this is the windows key (?).
Browser Compatibility
该
MouseEvent.metaKey
只读属性返回Boolean
表示如果Meta按下键(true
)否(false
)时发生了此事件。注意:在 Macintosh 键盘上,这是命令键 ( ?)。在 Windows 键盘上,这是 Windows 键 ( ?)。
浏览器兼容性
MDN claims MouseEvent.metaKey
is supported on FireFox and Chrome, but it's not working.
MouseEvent.metaKey
FireFox 和 Chrome 支持MDN 声明,但它不起作用。
Which key does MouseEvent.metaKey
refer to?
MouseEvent.metaKey
指的是哪个键 ?
Why is the above code not working?
为什么上面的代码不起作用?
回答by nnnnnn
If you're asking which key you would have to press on a Windows system in order for the MouseEvent
's metaKey
property to be true
, the answer is that it depends on the browser. And some Windows browsers simply don't support it and always return false
or undefined
.
如果您要问在 Windows 系统上必须按哪个键才能使MouseEvent
'smetaKey
属性成为true
,答案是这取决于浏览器。并且一些 Windows 浏览器根本不支持它并且总是返回false
or undefined
。
I could not find an up-to-date chart of browser support for metaKey
, though there is a really old one at QuirksMode.org.
我找不到浏览器支持的最新图表metaKey
,尽管在 QuirksMode.org 上有一个非常古老的图表。
If you are using jQuery, metaKey
is one of the event properties that it normalizes for cross-browser compatibility.
如果您使用的是 jQuery,它metaKey
是为跨浏览器兼容性而标准化的事件属性之一。
If you need to implement a key + mouse event for some functionality on your website, I would use the Shiftkey, so that it works on all systems. (If you need more than one key option, I would suggest you rethink your design.)
如果您需要为网站上的某些功能实现键 + 鼠标事件,我会使用该Shift键,以便它适用于所有系统。(如果您需要多个关键选项,我建议您重新考虑您的设计。)
回答by Orwellophile
Empirical testing, shows the following results. Not that jQuery doesn't do a terribly good job of normalizing ^F.
经验测试,显示以下结果。并不是说 jQuery 在规范化 ^F 方面做得不好。
On a Mac, in Safari Version 5.1.7 & 6.0.
在 Mac 上,在 Safari 版本 5.1.7 和 6.0 中。
F Keypress: 102, 102
?F Keypress: 102, 102 meta
?F Keypress: 402, 402 alt
?F Keypress: 6, 6 ctrl
?F Keypress: 70, 70 shift
On a Mac, in Firefox 15.0.1:
在 Mac 上,在 Firefox 15.0.1 中:
F Keypress: 102, 0
?F Keypress: 102, 0 meta
?F Keypress: 402, 0 alt
?F Keypress: 102, 0 ctrl
?F Keypress: 70, 0 shift
On a Mac, in Google Chrome 18.0.1024.168:
在 Mac 上,在 Google Chrome 18.0.1024.168 中:
F Keypress: 102, 102
?F (No triggers sent for ? + key)
?F Keypress: 402, 402 alt
?F Keypress: 6, 6 ctrl
?F Keypress: 70, 70 shift
Test code: // jquery-1.7.2
测试代码:// jquery-1.7.2
$(document.defaultView).keypress(function(e) {
console.log("Keypress: " + e.which + ", " + e.keyCode, " "
+ (e.metaKey ? "meta " : "")
+ (e.ctrlKey ? "ctrl " : "")
+ (e.altKey ? "alt " : "")
+ (e.shiftKey ? "shift " : ""));
});
回答by user10089632
Which key does MouseEvent.metaKey refer to?
MouseEvent.metaKey 指的是哪个键?
It refers to the windows key
它指的是windows键
Why is the above code not working?
为什么上面的代码不起作用?
Because of a bugas of at least Firefox 48, see the docsfor more infos.
由于至少在 Firefox 48中存在错误,请参阅文档以获取更多信息。
A solution :
一个解法 :
Use the shiftKeyinstead. Which has a property with the same name on the event object.
使用shiftKey来代替。它在事件对象上有一个同名的属性。