Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 708565 of hotkeys

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/hotkeys
  • Revision title: hotkeys
  • Revision id: 708565
  • Created:
  • Creator: Null
  • Is current revision? No
  • Comment
Tags: 

Revision Content

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

All Hotkey combinations require atleast one modifer-key+ (Control+key, Shift+key, Alt+key, Option+key, Command+key) or the following type error will occcur: "Hotkey must have at least one modifier"  

Hotkey selection should be choosen with care. It's very easy to overwrite other combinations that clash with hotkeys defined for Firefox or for other add-ons:                                                                                        Please reference the Firefox Keyboard shortcuts page for already used shortcuts.

Due to the nature of mainstream hotkey combinations already in heavy use the following Hotkeys will not pass the AMO Add-on 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"

Modifier keynames:

  • 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

Module exports Hotkey constructor allowing users to create a hotkey for the host application.

Methods

destroy()

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

Revision Source

<div class="note">
 <p>Stable</p>
</div>
<p><span class="seoSummary">Assign Hotkey combinations to functions in your add-on.</span></p>
<h2 id="Usage">Usage</h2>
<p>To define a hotkey combination, create a <code>Hotkey</code> 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:</p>
<pre class="brush: js">
// 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();
  }
});</pre>
<h3 id="Choosing_Hotkeys">Choosing Hotkeys</h3>
<p>All Hotkey combinations require atleast one <strong>modifer-</strong><em>key</em><strong>+</strong> (<strong>Control</strong>+<em>key</em>, <strong>Shift</strong>+<em>key</em>, <strong>Alt</strong>+<em>key</em>, <strong>Option</strong>+<em>key</em>, <strong>Command</strong>+<em>key</em>) or the following type error will occcur: "Hotkey must have at least one modifier" &nbsp;</p>
<p>Hotkey selection should be choosen with care. It's very easy to overwrite other combinations that clash with hotkeys defined for Firefox or for other add-ons: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Please reference the Firefox <u><a href="https://support.mozilla.org/en-US/kb/keyboard-shortcuts-perform-firefox-tasks-quickly">Keyboard shortcuts</a></u> page for already used shortcuts.</p>
<p>Due to the nature of mainstream hotkey combinations already in heavy use the following Hotkeys will not pass the AMO Add-on review:</p>
<pre>
<span style="color:#ff0000;">accel-Z</span>, <span style="color:#ff0000;">accel-C</span>, <span style="color:#ff0000;">accel-X</span>, <span style="color:#ff0000;">accel-V</span> or <span style="color:#ff0000;">accel-Q</span>
</pre>
<p>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, <code>accel-S</code> is typically used to save a file, but if you use it for something completely different, then it would be extremely confusing for users.</p>
<p>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.</p>
<h2 id="Globals">Globals</h2>
<h3 id="Constructors">Constructors</h3>
<h4 class="addon-sdk-api-name" id="Hotkey(options)"><code>Hotkey(options)</code></h4>
<p>Creates a hotkey whose <code>onPress</code> listener method is invoked when key combination defined by <code>hotkey</code> is pressed.</p>
<p>If more than one <code>hotkey</code> is created for the same key combination, the listener is executed only on the last one created.</p>
<h5 id="Parameters">Parameters</h5>
<p><strong>options : Object</strong><br />
 Required options:</p>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Name</th>
   <th scope="col">Type</th>
   <th scope="col">&nbsp;</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>combo</td>
   <td>String</td>
   <td>
    <p>Any function key: <code>"f1, f2, ..., f24"</code> or key combination in the format of <code>'modifier-key'</code>:</p>
    <pre class="brush: js">
"accel-s"
"meta-shift-i"
"control-alt-d"</pre>
    <p>Modifier keynames:</p>
    <ul>
     <li><strong>shift</strong>: The Shift key.</li>
     <li><strong>alt</strong>: The Alt key. On the Macintosh, this is the Option key. On Macintosh this can only be used in conjunction with another modifier, since <code>Alt-Letter</code> combinations are reserved for entering special characters in text.</li>
     <li><strong>meta</strong>: The Meta key. On the Macintosh, this is the Command key.</li>
     <li><strong>control</strong>: The Control key.</li>
     <li><strong>accel</strong>: 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.</li>
     <li><strong>pageup</strong>: The Page Up key.</li>
     <li><strong>pagedown</strong>: The Page Down key.</li>
    </ul>
   </td>
  </tr>
  <tr>
   <td>onPress</td>
   <td>Function</td>
   <td>
    <p>Function that is invoked when the key combination <code>hotkey</code> is pressed.</p>
   </td>
  </tr>
 </tbody>
</table>
<h2 id="Hotkey">Hotkey</h2>
<p>Module exports <code>Hotkey</code> constructor allowing users to create a <code>hotkey</code> for the host application.</p>
<h3 id="Methods">Methods</h3>
<h4 class="addon-sdk-api-name" id="destroy()"><code>destroy()</code></h4>
<p>Stops this instance of <code>Hotkey</code> from reacting on the key combinations. Once destroyed it can no longer be used.</p>
Revert to this revision