e.metaKey 在 JavaScript MouseEvent 中引用哪个键?

发布于 2024-11-14 02:03:01 字数 1112 浏览 1 评论 0原文

MouseEvent.metaKey 似乎不起作用。在 FireFox 和 Chrome 上,即使我在单击时按住 Win 键,它也会返回 false

<!doctype html>
<button onclick=alert(event.metaKey)>click while holding "meta key"</button>

MDN 声明

MouseEvent.metaKey 只读属性返回 布尔值 指示是否按下了 Meta 键 (true) 或未按下 (false)当 事件发生。

注意:在 Macintosh 键盘上,这是命令键 ()。在 Windows 上 键盘,这是 Windows 键 ()。

浏览器兼容性

在此处输入图像描述

MDN 声明 MouseEvent.metaKey 在 FireFox 和 Chrome 上受支持,但它不起作用。

MouseEvent.metaKey 指的是哪个键?

为什么上面的代码不起作用?

MouseEvent.metaKey doesn't seem to work. On both FireFox and Chrome, it returns false even if I hold the Win key while clicking:

<!doctype html>
<button onclick=alert(event.metaKey)>click while holding "meta key"</button>

MDN states:

The MouseEvent.metaKey read-only property returning a Boolean that
indicates if the Meta key 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

enter image description here

MDN claims MouseEvent.metaKey is supported on FireFox and Chrome, but it's not working.

Which key does MouseEvent.metaKey refer to?

Why is the above code not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑叹一世浮沉 2024-11-21 02:03:01

如果您询问在 Windows 系统上必须按哪个键才能使 MouseEventmetaKey 属性为 true,答案是这取决于浏览器。有些 Windows 浏览器根本不支持它,并且总是返回 falseundefined

我找不到浏览器对 metaKey 支持的最新图表,尽管确实有 QuirksMode.org 上的旧版本

如果您使用 jQuery,metaKey它规范化的事件属性之一用于跨浏览器兼容性

如果您需要为网站上的某些功能实现键 + 鼠标事件,我将使用 Shift 键,以便它适用于所有系统。 (如果您需要多个关键选项,我建议您重新考虑您的设计。)

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.

I could not find an up-to-date chart of browser support for metaKey, though there is a really old one at QuirksMode.org.

If you are using jQuery, metaKey is one of the event properties that it normalizes for cross-browser compatibility.

If you need to implement a key + mouse event for some functionality on your website, I would use the Shift key, so that it works on all systems. (If you need more than one key option, I would suggest you rethink your design.)

最美不过初阳 2024-11-21 02:03:01

实证测试显示以下结果。这并不是说 jQuery 在规范化 ^F 方面做得不好。

在 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 

在 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

在 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

测试代码:
// 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 " : ""));
  });

Empirical testing, shows the following results. Not that jQuery doesn't do a terribly good job of normalizing ^F.

On a Mac, in Safari Version 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:

 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:

 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

  $(document.defaultView).keypress(function(e) {
      console.log("Keypress: " + e.which + ", " + e.keyCode, " "
          + (e.metaKey ? "meta " : "")
          + (e.ctrlKey ? "ctrl " : "")
          + (e.altKey ? "alt " : "")
          + (e.shiftKey ? "shift " : ""));
  });
痴意少年 2024-11-21 02:03:01

MouseEvent.metaKey 指的是哪个键?

它指的是 Windows 键 Windows Key

为什么上面的代码不起作用?

由于至少从 Firefox 48 开始存在错误,请参阅文档了解更多信息信息。

解决方案:

使用 shiftKey 代替。其中在事件对象上有一个同名的属性。

Which key does MouseEvent.metaKey refer to?

It refers to the windows key Windows Key

Why is the above code not working?

Because of a bug as of at least Firefox 48, see the docs for more infos.

A solution :

Use the shiftKey instead. Which has a property with the same name on the event object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文