{"record":{"id":"6e8f39b858dfe169","repo":"nwjs/nw.js","slug":"invalid-key-format","errorCode":null,"errorMessage":"Invalid 'key' format.","messagePattern":"Invalid 'key' format\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/resources/api_nw_shortcut.js","lineNumber":116,"sourceCode":"\nvar handlers = {};\n\nfunction keyToAccelerator(key) {\n  key = key.toString();\n  var parts = key.split('+');\n  var maybeKey = parts.pop();\n  var maybeModifiers = parts;\n\n  var modifiers = {\n    alt: false,\n    command: false,\n    ctrl: false,\n    shift: false\n  };\n\n  maybeKey = ALIAS_MAP[maybeKey.toLowerCase()];\n  if (!maybeKey) {\n    throw new TypeError(OPTION_KEY_INVALID);\n  }\n  if (!maybeModifiers.every(function(m) {\n    return modifiers[m.toLowerCase()] = MODIFIERS_REG.test(m);\n  })) {\n    throw new TypeError(OPTION_KEY_INVALID);\n  }\n\n  return {key: maybeKey, modifiers: modifiers};\n}\n\nfunction normalizeLocal(accelerator) {\n  var modifiers = accelerator.modifiers;\n  return [modifiers.alt, modifiers.command, modifiers.ctrl, modifiers.shift, accelerator.key].join('-');\n}\n\nfunction getRegistryLocal(accelerator) {\n  var localKey = normalizeLocal(accelerator);\n  return handlers[localKey];","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/nwjs/nw.js/blob/e15da848e9e08e6e467532dae78995c6ad2f55ee/src/resources/api_nw_shortcut.js#L98-L134","documentation":"Thrown inside keyToAccelerator() when the final segment of a shortcut key string (the part after the last '+') does not resolve to a known key in ALIAS_MAP. ALIAS_MAP covers a-z, 0-9, F1-F24, arrows, punctuation aliases, and DOM Level-3 code names. An unrecognized key token means the accelerator cannot be mapped to a hardware code, so the Shortcut cannot be constructed or registered.","triggerScenarios":"new nw.Shortcut({ key: 'ctrl+xyz' }) where 'xyz' is not in ALIAS_MAP; passing a fully qualified code name that is misspelled like 'Digit0' vs 'digi0'; passing an empty string key after split such as 'ctrl+' (empty token); using a locale-specific key label not in the alias set.","commonSituations":"Typos in key strings; copying key names from non-NW.js documentation that uses different naming conventions; constructing keys dynamically from user input without validation; using uppercase code names like 'KEYA' instead of the expected 'KeyA' or 'a'.","solutions":["Verify the key token against ALIAS_MAP: use lowercase single chars (a-z, 0-9), lowercase code names (digit0, keya, arrowup), or symbolic aliases like 'enter', 'space', 'tab'.","Strip trailing '+' and ensure the key segment is non-empty before constructing the Shortcut.","If building key strings from user input, validate against a whitelist before passing to new nw.Shortcut()."],"exampleFix":"// before\nnew nw.Shortcut({ key: 'ctrl+xyz' });\n// after\nnew nw.Shortcut({ key: 'ctrl+a' });","handlingStrategy":"validation","validationCode":"// validate the key token before constructing\nconst ALIAS = /^[a-z0-9]|^f([1-9]|1[0-9]|2[0-4])$|^arrow(up|down|left|right)$|^(enter|space|tab|escape|backspace|delete|home|end|insert|pageup|pagedown)$/i;\nfunction validKeyToken(k) { return ALIAS.test(k); }","typeGuard":"function isValidShortcutKey(keyStr) {\n  if (typeof keyStr !== 'string') return false;\n  const parts = keyStr.split('+');\n  const last = parts.pop();\n  // coarse check: last segment must look like a key, modifiers must be known\n  return parts.every(m => /^(ctrl|alt|shift|command)$/i.test(m)) && last.length > 0;\n}","tryCatchPattern":"try {\n  const sc = new nw.Shortcut({ key: rawKey, active: fn });\n} catch (e) {\n  if (e.message === \"Invalid 'key' format.\") {\n    console.warn('Ignoring invalid shortcut key:', rawKey);\n  } else throw e;\n}","preventionTips":["Maintain a whitelist of allowed key names matching ALIAS_MAP.","Never build key strings from raw user input without validation.","Prefer single-char lowercase keys and the four canonical modifiers."],"tags":["nwjs","shortcut","validation","accelerator"],"backgroundTag":null,"analyzedSha":"e15da848e9e08e6e467532dae78995c6ad2f55ee","analyzedAt":"2026-08-13T04:15:35.452Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}