nwjs/nw.js · error · TypeError
Shortcut requires 'key' to specify key combinations.
Error message
Shortcut requires 'key' to specify key combinations.
What it means
After confirming the option is an object, the Shortcut constructor requires a `key` property (checked via hasOwnProperty) describing the key combination. Without it, a TypeError is thrown. The key is later coerced with String() and stored as `this.key`, then passed to the native accelerator registration.
Source
Thrown at src/api/shortcut/shorcut.js:28
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var v8_util = process.binding('v8_util');
function Shortcut(option) {
if (typeof option != 'object')
throw new TypeError('Invalid option.');
if (!option.hasOwnProperty('key'))
throw new TypeError("Shortcut requires 'key' to specify key combinations.");
option.key = String(option.key);
this.key = option.key;
if (option.hasOwnProperty('active')) {
if (typeof option.active != 'function')
throw new TypeError("'active' must be a valid function.");
else
this.active = option.active;
}
if (option.hasOwnProperty('failed')) {
if (typeof option.failed != 'function')
throw new TypeError("'failed' must be a valid function.");
else
this.failed = option.failed;
}
View on GitHub (pinned to e15da848e9)
Solutions
- Always include a `key` property with a valid accelerator string like 'Ctrl+Shift+P'.
- Validate the key is a non-empty string before constructing.
- Use the platform-correct modifier names (Ctrl/Alt/Shift plus a letter or function key).
Example fix
// before
new nw.Shortcut({ active: onActive }); // throws
// after
new nw.Shortcut({ key: 'Ctrl+Shift+P', active: onActive, failed: onFail }); Defensive patterns
Strategy: validation
Validate before calling
function buildShortcut(opt) {
if (!opt || !opt.hasOwnProperty('key'))
throw new TypeError("Shortcut requires 'key'");
if (typeof opt.key !== 'string' || !opt.key) {
opt.key = String(opt.key || '');
}
return new nw.Shortcut(opt);
} Type guard
function hasValidKey(opt) {
return !!opt && opt.hasOwnProperty('key') && typeof opt.key === 'string' && opt.key.length > 0;
} Try / catch
try { return new nw.Shortcut(opt); }
catch (e) {
if (e instanceof TypeError && /requires 'key'/.test(e.message)) {
opt.key = opt.key || defaultKey;
return new nw.Shortcut(opt);
} throw e;
} Prevention
- Always include a non-empty `key` string in the options.
- Validate the key before constructing the Shortcut.
- Use platform-correct modifier names (Ctrl/Alt/Shift + key).
When it happens
Trigger: Calling new nw.Shortcut({ active: fn }) with no key, or new nw.Shortcut({ failed: fn }). Also { key: undefined } explicitly (hasOwnProperty is true but the value is undefined) — this passes the check but yields an empty/unusable accelerator.
Common situations: Building the options object conditionally and forgetting the key branch. Passing only callbacks. Reading examples that highlight `active`/`failed` and omitting the required `key`.
Related errors
- A normal MenuItem must have a label
- Invalid option.
- Invalid option.
- Shortcut requires 'key' to specify key combinations.
- Invalid menu type: {option.type}
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/623c6a0840377915.
Report an issue: GitHub.