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 alway
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.