
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
ts-key-enum
Advanced tools
A TypeScript string enum for compile-time safety when working with event.key
A TypeScript string enum for compile-time safety when working with event.key
.
npm install ts-key-enum --save
Tired of referencing keyboard keys with a string?
onKeyPress = (ev) => {
// whoops, it's actually ArrowLeft!
if (ev.key === 'LeftArrow') {
...
}
}
Me too. With this module, you can do this instead (in a TypeScript file):
onKeyPress = (ev) => {
// much better
if (ev.key === Key.ArrowLeft) {
...
}
}
This is similar to the ts-keycode-enum module, but it provides an enum with string values that correspond with the event.key
values instead of number values that correspond to the deprecated event.which
and event.keyCode
values.
To use this module, import the Key
enum at the top of your TypeScript file:
import { Key } from 'ts-key-enum';
You can now use the enum value in place of key strings throughout the file:
// if (ev.key === 'Escape') { ... }
if (ev.key === Key.Escape) { ... }
See Key.enum.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.
The 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:
if (ev.key === 'é') { ... }
This package is published as two versions on NPM: v2.x
and v3.x
.
v2.x
defines the Key
enum as a "basic" enum:
export enum Key { ... }
The 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.
v3.x
defines the Key
enum as a const
enum:
export const enum Key { ... }
This 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 bundle.
It's advisable to use v3.x
unless you have a reason to use v2.x
. Some reasons you may need to use v2.x
:
1.4
, which doesn't support const enum
.@babel/plugin-transform-typescript
, which does not support const enum
.
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).To build this module yourself, first install its dependencies using
npm install
Next, run the scraper script (scrapeMDNForKeys.ts
) using
npm run scrape
This will overwrite Key.enum.ts
with the updated list of keys found in MDN.
Finally, run the build using
npm run build
Build output is placed in the dist
directory.
MIT
v2.0.13
Remove link to Wikipedia page on teletext.
FAQs
A TypeScript string enum for compile-time safety when working with event.key
We found that ts-key-enum demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.