{"id":16836,"library":"keycode-js","title":"Key Code Constants","description":"keycode-js is a JavaScript package providing a comprehensive set of constants for keyboard events, specifically mapping to KeyboardEvent.keyCode, KeyboardEvent.code, and KeyboardEvent.key properties. The current stable version is 3.1.0. The package maintains a steady release cadence, incorporating new key event codes and values while addressing compatibility and consistency across browser environments. A key differentiator is its provision of constants for all three KeyboardEvent properties, allowing developers to choose the most appropriate and future-proof method for handling keyboard input. It also ships with TypeScript types, enhancing developer experience in TypeScript projects, and offers direct support for Deno environments.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/kabirbaidhya/keycode-js","tags":["javascript","keycode","constants","typescript","deno","js","ts"],"install":[{"cmd":"npm install keycode-js","lang":"bash","label":"npm"},{"cmd":"yarn add keycode-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add keycode-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default imports for ES2015+ modules were removed in v2.0.0. Use `* as KeyCode` to import the module namespace object containing all constants.","wrong":"import KeyCode from 'keycode-js';","symbol":"KeyCode (wildcard import)","correct":"import * as KeyCode from 'keycode-js';"},{"note":"Use named imports to bring in specific constants, which is often preferred for tree-shaking and clarity.","symbol":"KEY_RETURN (named import)","correct":"import { KEY_RETURN } from 'keycode-js';"},{"note":"Use named imports for `KeyboardEvent.code` constants. These were added in v3.1.0.","symbol":"CODE_RETURN (named import)","correct":"import { CODE_RETURN } from 'keycode-js';"},{"note":"For CommonJS environments, the package exports its constants directly on the `module.exports` object.","symbol":"KeyCode (CommonJS)","correct":"const KeyCode = require('keycode-js');"}],"quickstart":{"code":"import * as KeyCode from 'keycode-js';\n\nwindow.addEventListener('keyup', function(e) {\n  // It's recommended to check 'code' or 'key' properties for modern browsers.\n  // 'keyCode' is deprecated by the W3C.\n\n  // Check the code value (e.g., 'Enter').\n  if (e.code === KeyCode.CODE_RETURN) {\n    console.log('It was the Return key using event.code.');\n    return;\n  }\n\n  // OR, check the key value (e.g., 'Enter').\n  if (e.key === KeyCode.VALUE_RETURN) {\n    console.log('It was the Return key using event.key.');\n    return;\n  }\n\n  // OR, check the keyCode value (e.g., 13) (deprecated).\n  if (e.keyCode === KeyCode.KEY_RETURN) {\n    console.log('It was the Return key using deprecated event.keyCode.');\n    return;\n  }\n\n  console.log('It was any other key:', e.key, e.code, e.keyCode);\n});","lang":"javascript","description":"Demonstrates how to import and use key code, key value, and key event constants from `keycode-js` within a browser's keyup event listener, showing both modern and deprecated approaches."},"warnings":[{"fix":"Review `KeyboardEvent.keyCode` usage. It's highly recommended to migrate to `KeyboardEvent.code` or `KeyboardEvent.key` with their corresponding `CODE_` or `VALUE_` constants for better cross-browser compatibility. If `keyCode` must be used, verify the new constant values against your expected behavior.","message":"In v3.0.0, `keyCode` constants were updated. This included changes to previously Gecko-specific constants to align more closely with general web standards. If your application relied on older, Firefox-specific `keyCode` values, their behavior or expected constant values might have changed.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Update your import statements. Change `import KeyCode from 'keycode-js';` to `import * as KeyCode from 'keycode-js';` to import the module namespace, or use named imports like `import { KEY_RETURN } from 'keycode-js';` for individual constants.","message":"Starting with v2.0.0, `keycode-js` no longer supports `default` imports (`import KeyCode from 'keycode-js'`) for ES2015+ modules. Attempting to use this syntax will result in an import error.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Prioritize the use of `e.code` with `KeyCode.CODE_EXAMPLE` constants or `e.key` with `KeyCode.VALUE_EXAMPLE` constants for handling keyboard events. These offer more reliable and future-proof identification of keys.","message":"The `KeyboardEvent.keyCode` property, which `keycode-js` provides `KEY_` constants for, is deprecated by the W3C and should be avoided in new web development. Modern alternatives include `KeyboardEvent.code` and `KeyboardEvent.key`.","severity":"deprecated","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change the import statement to `import * as KeyCode from 'keycode-js';` or `import { KEY_RETURN } from 'keycode-js';`.","cause":"Attempting to use a default import `import KeyCode from 'keycode-js'` in an ES module context with `keycode-js` v2.0.0 or later.","error":"TypeError: Cannot read properties of undefined (reading 'KEY_RETURN')"},{"fix":"Ensure the `keycode-js` script tag (`<script src=\"https://unpkg.com/keycode-js@latest/dist/keycode.min.js\"></script>`) is loaded before your script attempts to use `KeyCode`. In modules, ensure it's properly imported.","cause":"Trying to use `KeyCode` constants in a browser `<script>` tag before the library is loaded, or if the global `KeyCode` object is not accessible in the current scope.","error":"Error: `KeyCode` is not defined"}],"ecosystem":"npm","meta_description":null}