
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
key-event-to-string
Advanced tools
Converts a JavaScript key event object into a humanly readable format
This library helps converting the event object of a JavaScript keydown event into a humanly readable format. The idea is to use this for UI components that let the user choose keyboard shortcuts.
In other words: This library provides the inverse functionality to common keyboard shortcut binding libraries like keymaster or Mousetrap.
$ npm install --save key-event-to-string
var event2string = require('key-event-to-string')(options)
document.body.onkeydown = (e) => {
var keys = event2string(e)
console.log(keys) // e.g. "Ctrl + A"
}
options
is optional and can be an object with the following properties:
key | value | default value |
---|---|---|
cmd | What string to display for the Cmd/Meta modifier | "Cmd" |
ctrl | What string to display for the Ctrl modifier | "Ctrl" |
alt | What string to display for the Alt/Option modifier | "Alt" |
shift | What string to display for the Shift modifier | "Shift" |
joinWith | The string that's displayed between all keys | " + " |
For example this could be used to get the Mac style keyboard shortcut strings:
{
cmd: "β",
ctrl: "β",
alt: "β₯",
shift: "β§",
joinWith: ""
}
The default settings are compatible with the format that common keyboard shortcut libraries, like keymaster or Mousetrap, accept.
require('key-event-to-string').details(e)
can be used to get more details. This can be useful for
validating keyboard shortcuts, e.g. for requiring a modifier and a normal key.
It returns an object with this information:
hasModifier
: True iff atleast one of cmd, ctrl, alt or shift was pressedhasKey
: True iff a key other than a modifier is pressedmap
: An object containing information which modifier is active and what
other key is pressedkeydown
events. keypress
/ keyup
events have small differences, e..g. keydown
is needed to capture Command
on a Mac. So keydown
is advisible for this anyways.ΓΆ
has the same keyCode as ;
, on a German keyboard. This library doesn't try to fix that and I don't think there's a good fix for all those special cases. Other keyboard shortcut libraries (Mousetrap/keymaster e.g.) have the same problem, so it shouldn't be a big problem since this library is meant to be used as a helper for those librariesFAQs
Converts a JavaScript key event object into a humanly readable format
The npm package key-event-to-string receives a total of 294 weekly downloads. As such, key-event-to-string popularity was classified as not popular.
We found that key-event-to-string demonstrated a not healthy version release cadence and project activity because the last version was released 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.