{"record":{"id":"623c6a0840377915","repo":"nwjs/nw.js","slug":"shortcut-requires-key-to-specify-key-combination","errorCode":null,"errorMessage":"Shortcut requires 'key' to specify key combinations.","messagePattern":"Shortcut requires 'key' to specify key combinations\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/api/shortcut/shorcut.js","lineNumber":28,"sourceCode":"//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM\n// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES\n// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS\n// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH\n// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nvar v8_util = process.binding('v8_util');\n\nfunction Shortcut(option) {\n  if (typeof option != 'object')\n    throw new TypeError('Invalid option.');\n\n  if (!option.hasOwnProperty('key'))\n    throw new TypeError(\"Shortcut requires 'key' to specify key combinations.\");\n\n  option.key = String(option.key);\n  this.key = option.key;\n\n  if (option.hasOwnProperty('active')) {\n    if (typeof option.active != 'function')\n      throw new TypeError(\"'active' must be a valid function.\");\n    else\n      this.active = option.active;\n  }\n\n  if (option.hasOwnProperty('failed')) {\n    if (typeof option.failed != 'function')\n      throw new TypeError(\"'failed' must be a valid function.\");\n    else\n      this.failed = option.failed;\n  }\n","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/nwjs/nw.js/blob/e15da848e9e08e6e467532dae78995c6ad2f55ee/src/api/shortcut/shorcut.js#L10-L46","documentation":"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.","triggerScenarios":"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.","commonSituations":"Building the options object conditionally and forgetting the key branch. Passing only callbacks. Reading examples that highlight `active`/`failed` and omitting the required `key`.","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)."],"exampleFix":"// before\nnew nw.Shortcut({ active: onActive }); // throws\n\n// after\nnew nw.Shortcut({ key: 'Ctrl+Shift+P', active: onActive, failed: onFail });","handlingStrategy":"validation","validationCode":"function buildShortcut(opt) {\n  if (!opt || !opt.hasOwnProperty('key'))\n    throw new TypeError(\"Shortcut requires 'key'\");\n  if (typeof opt.key !== 'string' || !opt.key) {\n    opt.key = String(opt.key || '');\n  }\n  return new nw.Shortcut(opt);\n}","typeGuard":"function hasValidKey(opt) {\n  return !!opt && opt.hasOwnProperty('key') && typeof opt.key === 'string' && opt.key.length > 0;\n}","tryCatchPattern":"try { return new nw.Shortcut(opt); }\ncatch (e) {\n  if (e instanceof TypeError && /requires 'key'/.test(e.message)) {\n    opt.key = opt.key || defaultKey;\n    return new nw.Shortcut(opt);\n  } throw e;\n}","preventionTips":["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)."],"tags":["nwjs","shortcut","constructor","validation","required-field"],"backgroundTag":null,"analyzedSha":"e15da848e9e08e6e467532dae78995c6ad2f55ee","analyzedAt":"2026-08-13T04:15:35.452Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}