{"id":20295013,"url":"https://github.com/nfriend/ts-key-enum","last_synced_at":"2025-07-27T09:35:48.357Z","repository":{"id":32568648,"uuid":"137214505","full_name":"nfriend/ts-key-enum","owner":"nfriend","description":"A TypeScript string enum for compile-time safety when working with event.key","archived":false,"fork":false,"pushed_at":"2022-12-07T19:33:53.000Z","size":194,"stargazers_count":117,"open_issues_count":4,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T12:19:10.766Z","etag":null,"topics":["npm","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ts-key-enum","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nfriend.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-13T12:46:27.000Z","updated_at":"2025-01-09T03:56:52.000Z","dependencies_parsed_at":"2023-01-14T21:37:03.408Z","dependency_job_id":null,"html_url":"https://github.com/nfriend/ts-key-enum","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nfriend/ts-key-enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfriend%2Fts-key-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfriend%2Fts-key-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfriend%2Fts-key-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfriend%2Fts-key-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nfriend","download_url":"https://codeload.github.com/nfriend/ts-key-enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nfriend%2Fts-key-enum/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264502493,"owners_count":23618619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["npm","typescript"],"created_at":"2024-11-14T15:32:42.114Z","updated_at":"2025-07-09T19:38:32.718Z","avatar_url":"https://github.com/nfriend.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-key-enum\n\n\u003ca href=\"https://gitlab.com/nfriend/ts-key-enum/pipelines/latest\" target=\"_blank\"\u003e\n    \u003cimg alt=\"GitLab build status\" src=\"https://gitlab.com/nfriend/ts-key-enum/badges/master/pipeline.svg\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://www.npmjs.com/package/ts-key-enum\" target=\"_blank\"\u003e\n    \u003cimg alt=\"npm version\" src=\"https://img.shields.io/npm/v/ts-key-enum\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://www.npmjs.com/package/ts-key-enum\" target=\"_blank\"\u003e\n    \u003cimg alt=\"types\" src=\"https://img.shields.io/npm/types/ts-key-enum\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://www.npmjs.com/package/ts-key-enum\" target=\"_blank\"\u003e\n    \u003cimg alt=\"npm downloads\" src=\"https://img.shields.io/npm/dw/ts-key-enum\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://gitlab.com/nfriend/ts-key-enum/blob/master/LICENSE\" target=\"_blank\"\u003e\n    \u003cimg alt=\"license\" src=\"https://img.shields.io/npm/l/ts-key-enum\"\u003e\n\u003c/a\u003e\n\nA TypeScript string enum for compile-time safety when working with `event.key`.\n\n\u003cimg align=\"right\" src=\"https://raw.githubusercontent.com/nfriend/ts-key-enum/master/logo.jpg\" /\u003e\n\n## Install\n\n```sh\nnpm install ts-key-enum --save\n```\n\n```sh\nyarn add ts-key-enum\n```\n\n## Purpose\n\nTired of referencing keyboard keys with a string?\n\n```js\nonKeyPress = (ev) =\u003e {\n\n    // whoops, it's actually ArrowLeft!\n    if (ev.key === 'LeftArrow') {\n        ...\n    }\n}\n```\n\nMe too. With this module, you can do this instead (in a TypeScript file):\n\n```ts\nonKeyPress = (ev) =\u003e {\n\n    // much better\n    if (ev.key === Key.ArrowLeft) {\n        ...\n    }\n}\n```\n\n## Background\n\nThis is similar to the [ts-keycode-enum](https://github.com/nfriend/ts-keycode-enum) module, but it provides an enum with string values that correspond with the [`event.key` values](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) instead of number values that correspond to the deprecated [`event.which`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which) and [`event.keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) values.\n\n## Usage\n\nTo use this module, import the `Key` enum at the top of your TypeScript file:\n\n```js\nimport { Key } from 'ts-key-enum';\n```\n\nYou can now use the enum value in place of key strings throughout the file:\n\n```js\n// if (ev.key === 'Escape') { ... }\nif (ev.key === Key.Escape) { ... }\n```\n\nSee [`Key.enum.d.ts`](./Key.enum.d.ts) for a complete list of available keys. This file is auto-generated from the list of keys found at MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values.\n\n## What's included\n\nThe `Key` enum contains values for all standard non-printable keys such as \"CapsLock\", \"Backspace\", and \"AudioVolumeMute\". The enum does _not_ contain values for printable keys such as \"a\", \"A\", \"#\", \"é\", or \"¿\", simply because the list of possible values is too vast to include in a single enum. To test for printable values, simply use a string comparison:\n\n```js\nif (ev.key === 'é') { ... }\n```\n\n## v2 vs v3\n\nThis package is published as two versions on [NPM](https://www.npmjs.com/package/ts-key-enum): `v2.x` and `v3.x`.\n\n### v2\n\n`v2.x` defines the `Key` enum as [a \"basic\" enum](https://www.typescriptlang.org/docs/handbook/enums.html#enums):\n\n```ts\nexport enum Key { ... }\n```\n\nThe end result is a JavaScript object that contains every enum value. You can see this object here: https://gitlab.com/nfriend/ts-key-enum/blob/v2/dist/js/Key.enum.js.\n\n### v3\n\n`v3.x` defines the `Key` enum as [a `const` enum](https://www.typescriptlang.org/docs/handbook/enums.html#const-enums):\n\n```ts\nexport const enum Key { ... }\n```\n\nThis allows the enum's definition to live entirely in the definition file `https://gitlab.com/nfriend/ts-key-enum/blob/master/Key.enum.d.ts`. Consumers can use this enum without including _all_ of the enum's values in their own JavaScript bundle; only the values referenced in their code will be injected into their output file.\n\n## Which version should I use?\n\nIt's advisable to use `v3.x` unless you have a reason to use `v2.x`. Some reasons you may need to use `v2.x`:\n\n- You are using this package as a JavaScript module (not a TypeScript module).\n- You are using TypeScript \u003c `1.4`, which doesn't support `const enum`.\n- You are using [`@babel/plugin-transform-typescript`](https://babeljs.io/docs/en/babel-plugin-transform-typescript), which [does not support `const enum`](https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats).\n  - See #2 and https://github.com/babel/babel/issues/8741\n  - Alternatively, use [`babel-plugin-const-enum`](https://www.npmjs.com/package/babel-plugin-const-enum) to convert `const enum`s to regular `enum` in your development environment, allowing you to reap the benefits of `const enum`s in your production build (see https://www.npmjs.com/package/babel-plugin-const-enum#transform-removeconst-default).\n\n## Building\n\nTo build this module yourself, first install its dependencies using:\n\n```sh\nnpm install\n```\n\nNext, run the scraper script ([`scrapeMDNForKeys.ts`](./scrapeMDNForKeys.ts)) using:\n\n```sh\nnpm run scrape\n```\n\nThis will overwrite [`Key.enum.d.ts`](./Key.enum.d.ts) with the updated list of keys found in MDN.\n\nVerify that the enum builds without any TypeScript errors:\n\n```sh\nnpm run build\n```\n\nFinally, make sure the enum passes all linters:\n\n```sh\nnpm run lint\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfriend%2Fts-key-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnfriend%2Fts-key-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnfriend%2Fts-key-enum/lists"}