hotkeys

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Stable

Assign hotkey combinations to functions in your add-on.

Usage

To define a hotkey combination, create a Hotkey object, passing it the combination and a function to be called when the user presses that combination. For example, this add-on defines two hotkey combinations, to show and hide a panel:

// Define keyboard shortcuts for showing and hiding a custom panel.
var { Hotkey } = require("sdk/hotkeys");
var showHotKey = Hotkey({
  combo: "accel-shift-o",
  onPress: function() {
    showMyPanel();
  }
});
var hideHotKey = Hotkey({
  combo: "accel-alt-shift-o",
  onPress: function() {
    hideMyPanel();
  }
});

Choosing Hotkeys

Hotkeys should be chosen with care. It's very easy to use combinations that clash with hotkeys defined for Firefox or for other add-ons.

The following commonly used hotkey combinations will not pass AMO review:

accel-Z, accel-C, accel-X, accel-V or accel-Q

If you choose to use a key combination that's already defined, choose one which makes sense for the operation it will perform. For example, accel-S is typically used to save a file, but if you use it for something completely different, then it would be extremely confusing for users.

No matter what you choose, it's likely to annoy some people, and to clash with some other add-on, so consider making the combination you choose user-configurable.

Globals

Constructors

Hotkey(options)

Creates a hotkey whose onPress listener method is invoked when key combination defined by hotkey is pressed.

If more than one hotkey is created for the same key combination, the listener is executed only on the last one created.

Parameters

options : Object
Required options:

Name Type  
combo String

Any function key: "f1, f2, ..., f24" or key combination in the format of 'modifier-key':

"accel-s"
"meta-shift-i"
"control-alt-d"

All Hotkeys require at least one modifier as well as the key. These are the possible modifiers you can supply:

  • shift: The Shift key.
  • alt: The Alt key. On the Macintosh, this is the Option key. On Macintosh this can only be used in conjunction with another modifier, since Alt-Letter combinations are reserved for entering special characters in text.
  • meta: The Meta key. On the Macintosh, this is the Command key.
  • control: The Control key.
  • accel: The key used for keyboard shortcuts on the user's platform, which is Control on Windows and Linux, and Command on Mac. Usually, this would be the value you would use.
  • pageup: The Page Up key.
  • pagedown: The Page Down key.
onPress Function

Function that is invoked when the key combination hotkey is pressed.

Hotkey

Methods

destroy()

Stops this instance of Hotkey from reacting to the key combinations. Once destroyed a Hotkey can no longer be used.

Document Tags and Contributors

Tags: 
 Contributors to this page: wbamberg, Canuckistani, Null, zombie, camolin3
 Last updated by: wbamberg,