Type | Object |
---|---|
Mandatory | No |
Example | "commands": { "toggle-feature": { "suggested_key": { "default": "Ctrl+Shift+Y", "linux": "Ctrl+Shift+U" }, "description": "Send a 'toggle-feature' event" } } |
Use the commands
key to define one or more keyboard shortcuts for your extension.
Each keyboard shortcut is defined with a name, a combination of keys, and a description. Once you've defined commands in your extension's manifest.json
, you can listen for their associated key combinations with the commands
JavaScript API.
The commands
key is an object, and each shortcut is a property of it. The property's name is the name of the shortcut.
Each shortcut's value is an object, with up to 2 properties:
suggested_key
: the combination of keys that activate the shortcut.description
: a string that describes the shortcut; i.e. what it does.The suggested_key
property is an object with any of the following properties (all strings):
"default"
"mac"
"linux"
"windows"
"chromeos"
"android"
"ios"
The value of each property is the keyboard shortcut for the command on that platform, as a string containing keys separated by "+
". The value for "default"
is used on all platforms that are not explicitly listed.
For example:
"commands": { "toggle-feature": { "suggested_key": { "default": "Alt+Shift+U", "linux": "Ctrl+Shift+U" }, "description": "Send a 'toggle-feature' event to the extension" }, "do-another-thing": { "suggested_key": { "default": "Ctrl+Shift+Y" } } }
This JSON defines 2 shortcuts:
"toggle-feature"
, accessed with Ctrl+Shift+U on Linux, and Alt+Shift+U on all other platforms."do-another-thing"
, accessed with Ctrl+Shift+Y on all platforms.You could then listen for the "toggle-feature"
command with code like this:
browser.commands.onCommand.addListener(function (command) { if (command === "toggle-feature") { console.log("Toggling the feature!"); } });
There are 3 special shortcuts with default actions, for which the commands.onCommand
event is not fired:
_execute_browser_action
: works like a click on the extension's browser action._execute_page_action
: works like a click on the extension's page action._execute_sidebar_action
: opens the extension's sidebar. Only supported in Firefox 54 and newer.For example, this JSON defines a key combination that clicks the extension's browser action:
"commands": { "_execute_browser_action": { "suggested_key": { "default": "Ctrl+Shift+Y" } } }
There are two valid formats for shortcut keys: as a key combination or as a media key.
Note: On Macs, "Ctrl"
is interpreted as "Command"
, so if you actually need "Ctrl"
, specify "MacCtrl"
.
Key combinations must consist of 2 or 3 keys:
"Ctrl"
, "Alt"
, "Command"
, or "MacCtrl"
."Shift"
or (for Firefox ≥ 63) any one of "Ctrl"
, "Alt"
, "Command"
, or "MacCtrl"
. Must not be the modifier already used as the main modifier.A
–Z
0
–9
F1
–F12
Comma
, Period
, Home
, End
, PageUp
, PageDown
, Space
, Insert
, Delete
, Up
, Down
, Left
, Right
The key is then given as a string containing the set of key values, in the order listed above, separated by "+
". For example, "Ctrl+Shift+Z"
.
If a key combination is already used by the browser (like "Ctrl+P"
) or by an existing add-on, then you can't override it. You can define it, but your event handler will not be called when the user presses the key combination.
Alternatively, the shortcut may be specified as one of the following media keys:
"MediaNextTrack"
"MediaPlayPause"
"MediaPrevTrack"
"MediaStop"
Shortcuts can be updated via commands.update()
. Users can also update shortcuts via the "Manage Extension Shortcuts" option at about:addons
in Firefox, as shown in this video. In Chrome, users can change shortcuts at chrome://extensions/shortcuts
.
Define a single keyboard shortcut, using only the default key combination:
"commands": { "toggle-feature": { "suggested_key": { "default": "Ctrl+Shift+Y" }, "description": "Send a 'toggle-feature' event" } }
Define two keyboard shortcuts, one with a platform-specific key combination:
"commands": { "toggle-feature": { "suggested_key": { "default": "Alt+Shift+U", "linux": "Ctrl+Shift+U" }, "description": "Send a 'toggle-feature' event" }, "do-another-thing": { "suggested_key": { "default": "Ctrl+Shift+Y" } } }
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
commands |
Yes |
79 |
48 |
? |
Yes |
14
Ability to change the keyboard shortcut for a command not supported.
|
? |
? |
No |
? |
? |
? |
F1-F12 |
Yes |
79 |
53 |
? |
Yes |
14 |
? |
? |
No |
? |
? |
? |
MediaNextTrack |
Yes |
79 |
57
["macOS support requires the
global command feature which isn't supported yet. See Bug 1251795.", "Can only register one Media* command at a time. See Bug 1251795."] |
? |
Yes |
No |
? |
? |
No |
? |
? |
? |
MediaPlayPause |
Yes |
79 |
57
["macOS support requires the
global command feature which isn't supported yet. See Bug 1251795.", "Can only register one Media* command at a time. See Bug 1251795."] |
? |
Yes |
No |
? |
? |
No |
? |
? |
? |
MediaPrevTrack |
Yes |
79 |
57
["macOS support requires the
global command feature which isn't supported yet. See Bug 1251795.", "Can only register one Media* command at a time. See Bug 1251795."] |
? |
Yes |
No |
? |
? |
No |
? |
? |
? |
MediaStop |
Yes |
79 |
57
["macOS support requires the
global command feature which isn't supported yet. See Bug 1251795.", "Can only register one Media* command at a time. See Bug 1251795."] |
? |
Yes |
No |
? |
? |
No |
? |
? |
? |
MoreSecondaryModifiers |
No |
No |
63 |
? |
No |
14 |
? |
? |
No |
? |
? |
? |
global |
Yes |
79 |
No |
? |
Yes |
No |
? |
? |
No |
? |
? |
? |
_execute_sidebar_action |
No |
No |
54 |
? |
No |
No |
? |
? |
No |
? |
? |
? |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/commands