Firefox能检测MetaKey吗? [英] Can Firefox detect MetaKey?

查看:113
本文介绍了Firefox能检测MetaKey吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Firefox扩展,并与元键(我的键盘上的 Windows 键)纠缠在一起。 KeyboardEvent.metaKey 应该检测到它,但总是返回false。 (您可以在



Firefox的典型事件 - z b
$ b



Chrome针对Windows的典型事件 - z p>



使用 Windows 键作为修饰符:

正如您在上面的屏幕截图中所看到的,您不会获得 Windows的完整序列 - 在Firefox或Chrome中的 z 。与其他关键组合相比, z 缺少 keydown 事件。为什么这个事件没有被解雇我不清楚。我怀疑它是被Windows吞噬的。

使用 Windows 键会有问题。 Windows会对许多按键组合作出反应,包括那些只是由用户执行的轻微错误键,而不是按照需要小心。您可能最好使用不同的修饰键,或者不包含 Windows 键的修饰键组合。

另外一个例子: kbd> Alt - Windows - z 会导致3 keydown 事件的完整序列, Firefox中的code> keypress 事件和3 keyup 事件,而在Chrome中,顺序不会触发键盘事件。



一般的键盘快捷键:

键盘组合分配给不同的任务,从操作系统到操作系统,浏览器到浏览器,以及用户到用户。一般来说,如果您想要在通常会发布的加载项中使用键盘快捷方式,最佳操作方法是使用默认键盘快捷方式,但允许用户选择为每个操作使用哪个快捷方式,或者即使有一个行动的捷径。实际情况是,用户会遇到与其他应用程序和浏览器扩展冲突。取决于您正在编写的扩展类型,更改键盘快捷键可能会,也可能不会,现在可能。


I'm working on a Firefox extension and struggle with the meta key (Windows key on my keyboard). KeyboardEvent.metaKey is supposed to detect it but always returns false. (You can try it out on MDN or even easier in the jQuery API docs.)

I can reproduce this with Firefox 48.0 on KDE 16.08 / Gentoo and Firefox 47.0.1 on Windows 10. I also tried all combinations of Ctrl, Shift, and Alt but nothing registered as meta.

Chrome 53.0 does not have that problem, btw.

解决方案

No, and Yes:
Yes, Firefox can detect the Windows key, but not as the "Meta" key. Both keyup and keydown events are fired for that key, but not a keypress event. Chrome also fires events similarly (no keypress). The keyCode is the same on both Chrome and Firefox, but what they consider the key to be is different between the two. Chrome considers the key to be the "Meta" key, while Firefox considers it to be the "OS" key. Thus, Firefox does not set the metaKey property to true, because from Firefox's point of view, the "Meta" key is not being pressed.

Firefox's current interpretation of the Windows key (and probably the Mac Command, but I have not tested) is contrary to the documentation at KeyboardEvent.metaKey. Either the Firefox code needs to change, or the documentation. At a minimum, the documentation should state the current situation. Without explanation of why this behavior is currently this way, I would consider it a bug in Firefox. Meanwhile, I have updated the KeyboardEvent.metaKey page on MDN with a note that the Windows key does cause metaKey to be true (you may have to press Ctrl-F5 to refresh the page).

In harmonizing the documentation page for nsIDOMWindowUtils with the source code (nsIDOMWindowUtils, UIEvent.cpp), I noticed in the source that additional modifier codes had been added, including MODIFIER_OS to indicate the Windows key separate from a "Meta" key. I did not look into when exactly this change was made.

If you want to know that the Windows key was pressed when an event you are handling occurred, you will have to use handlers on both keydown and keyup that watch for and track the current state of the Windows key.

Typical Windows events (keydown shown in full):

The following was the console log after pressing and releasing the Windows key while monitoring keydown, keypress input and keyup events.

Firefox Typical events for Windows-z:

Chrome Typical events for Windows-z:

Using the Windows key as a modifier:
As you can see in the above screenshots, you don't get a full sequence of events for Windows-z in either Firefox or Chrome. Compared to other key combos both are missing the keydown event for the z. Why that event was not fired is not clear to me. I suspect that it was swallowed by Windows.

Using the Windows key will be problematic. Windows will react to many key combinations including ones that are just slight mis-keyings performed by a user that is not being quite as careful as required. You are probably better off using a different modifier key, or combination of modifier keys that does not include the Windows key.

Another example: Alt-Windows-z results in a full sequence of 3 keydown events, a keypress event, and 3 keyup events in Firefox, whereas in Chrome the sequence does not fire the keypress event.

Keyboard Shortcuts in general:
The key combinations that are already assigned to different tasks vary from OS to OS, browser to browser, and user to user. In general, if you are wanting to use a keyboard shortcut in an add-on which you are generally releasing, the best course of action is to have default keyboard shortcuts, but allow the user the option to define what shortcut to use for each action, or even if there is a shortcut for the action. The reality is that users will run into conflicts with other applications and browser extensions. Depending on what type of extension you are writing, changing keyboard shortcuts may, or may not, be possible at this time.

这篇关于Firefox能检测MetaKey吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆