tinykeys: a ~650 byte keybinding library that matches KeyboardEvent.key and KeyboardEvent.code
A tiny (~650 B) & modern library for keybindings.
At a glance
- What is it?
- tinykeys is a small TypeScript library for binding keyboard shortcuts in the browser, with sequence support and a $mod alias for cross-platform bindings. It is a good fit for apps that need a few shortcuts without a framework-sized dependency, and a poor fit for anyone who needs a documented upgrade path, since the last push was on 2026-05-26.
- Who is it for?
- Adopt tinykeys if you need a handful of keyboard shortcuts in a browser app and want a library you can read end to end. Do not adopt it if you need a documented deprecation path, a release history, or server-side key handling, none of which the README covers.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 58 days ago.
- What is it written in?
- Mainly TypeScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 22, 2026, and from our analysis. They are not legal advice.
Star history
- jamiebuilds/tinykeys4,109 ★ · +9
| Repository | 2026-09-23 | 2026-11-22 |
|---|---|---|
| jamiebuilds/tinykeys | 4.1k | 4.1k |
9 Stars in the last 60 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The problem tinykeys solves: shortcut handling without a framework
Binding a keyboard shortcut in the browser is not hard. Binding ten of them, on two operating systems, without the handlers stepping on each other, is where code starts to sprawl. The usual homegrown version accumulates a keydown listener, a map of key strings, a normalisation step for Meta versus Control, and a growing pile of conditionals for sequences like "g then i".
tinykeys targets that middle ground. It is not a command palette, not a focus manager, and not a shortcut editor UI. It is the matching layer: you hand it an object whose keys are binding strings and whose values are callbacks, and it decides which callback fires for a given KeyboardEvent. The README describes it as "a tiny (~1KB) & modern library for keybindings", and package.json puts the version at 4.0.0.
The intended audience is an application developer who already has a DOM event to hang the handler on. That can be window, as in every README example, or an element you choose yourself when you use createKeybindingsHandler and call addEventListener directly. The library does not own the listener lifecycle in that second mode, which is the point.
Matching on key and code, and why the $mod alias exists
A press in tinykeys is matched against two properties of the event: KeyboardEvent.key and KeyboardEvent.code, case-insensitively. So "d" matches event.key and "KeyD" matches event.code. The README includes a table mapping physical keys to both values, because the names are not always what you would guess: the Space key has no key value listed, Enter is Enter for key and Enter for code, and the digit row is 1, 2 for key but Digit1, Digit2 for code.
Modifiers match against KeyboardEvent.getModifierState(), and any modifier can be wrapped in brackets to make it optional, as in "Control+[Shift]+D". The $mod prefix is the cross-platform shorthand: the README states that on Mac $mod equals Meta and on Windows and Linux it equals Control. That single alias removes the most common source of duplicated binding tables.
Parentheses enable case-insensitive regular expressions for the key itself, not for modifiers. The README gives "$mod+([0-9])" as the example, equivalent to the regex /^(?:[0-9])$/iv. Sequences are space-separated presses, and the README states that each press must occur within 1000ms of the last. That timeout is fixed, not configurable in the documented API, which is worth knowing before you design a two-key navigation scheme around it.
Installing tinykeys and writing a first binding
The README gives one install path, npm, and notes that a CDN build is available on unpkg.com. The package is ESM-first: package.json sets "type": "module" and declares engines of node >=22, so check your toolchain before assuming an older Node will build it.
npm install --save tinykeysWith the dependency in place, import tinykeys and pass it a target plus a binding object. The README's own example binds Shift+D and uses event.preventDefault() inside the callback, which is how you stop the browser's default handling for the combination.
import { tinykeys } from "tinykeys"
tinykeys(window, {
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
})After this runs, pressing Shift and D together fires the first callback, and typing y, e, e, t in order within the sequence timeout fires the second. If you would rather register the listener yourself, import createKeybindingsHandler instead, build the handler from the same binding object, and attach it with window.addEventListener("keydown", handler).
In a React component the README recommends using the unsubscribe function that tinykeys returns as the cleanup value of a useEffect, so the listener is removed when the component unmounts.
import { useEffect } from "react"
import { tinykeys } from "tinykeys"
useEffect(() => {
return tinykeys(window, {
"$mod+b": formatBold,
"$mod+i": formatItalic,
})
}, [])The returned function is the entire teardown story. There is no registry to query and no way to enumerate the bindings you have installed.
Binding order is the sharp edge, and AltGraph is the second one
The README is explicit that the order of keybindings matters: if two bindings would match the same event, only the first fires. The documented case is a table containing both "$mod+b" and "$mod+KeyB". A user holding $mod and pressing b triggers only the first entry, and the second callback never runs. That is a silent failure. Nothing warns you, and the symptom is a shortcut that works for some keyboard layouts and not others.
The AltGraph alias is the other place where real keyboards diverge from the tidy example. On many non-US Windows layouts, and sometimes on macOS, pressing Control+Alt is reported by the browser as AltGraph instead. tinykeys aliases these so that a binding written as "Control+Alt+KeyS" also catches the AltGraph variant. The README's own advice is to prefer event.code for letters in this situation, which is why the example uses KeyS rather than S.
The README also flags that some keys share a code because they sit on the same physical key, and that international layouts shift this further. There is a key logger on the demo site for checking what your own keyboard reports. That is the honest position: the library normalises what it can and tells you to verify the rest.
Where tinykeys is the wrong choice
If you need a documented upgrade path, the README does not provide one. It documents no rollback behaviour and no migration notes between versions, and no release history came back for the repository. The last push to the default branch was on 2026-05-26. That is recent enough that calling the project abandoned would be wrong, but there is nothing here to tell you what changed in 4.0.0 or what a future major might break.
If your shortcuts need scoping, tinykeys gives you no built-in notion of an active region. Every binding on a given target is live while that listener is attached, so you implement modal editing, input-field exclusion, or per-panel shortcut sets yourself. A library that ships a scope stack will be less code for that job.
If you need a shortcut editor, conflict detection, or a settings UI, none of that exists here. The README does not document a way to list installed bindings, which is the primitive such a UI would need. And if your handlers belong on the server rather than in a browser, this is the wrong layer entirely: it is built around KeyboardEvent and getModifierState(), both browser APIs.
How tinykeys differs from a declarative shortcut component
The closest alternative in the JavaScript ecosystem is mousetrap, which the package keywords list names alongside react, vue, angular and ember. The approaches differ in where the binding lives. mousetrap exposes a global bind call that registers into its own internal registry, and the library owns the listener. tinykeys takes a plain object and either attaches the listener for you or hands you a handler function to attach yourself, with no global state between calls.
That difference has consequences. With tinykeys, two independent modules can each call tinykeys(window, {...}) and neither knows about the other, so a collision is resolved by whichever listener was registered first, not by any central table. With a registry-based library, a single place knows every binding and can in principle detect conflicts. Neither is strictly better; the tinykeys model is easier to tree-shake and to reason about per component, and the registry model is easier to inspect at runtime.
The second difference is size and scope. tinykeys is described as roughly 1KB and its package.json declares "sideEffects": false, so bundlers can drop it when unused. A library that carries a registry, a browser-compatibility shim, and a plugin surface will be larger and will keep more of itself alive.
Licence and the cost of keeping it current
tinykeys is MIT licensed, and the LICENSE file sits at the top level of the repository. For most applications that means you can use it, modify it, and ship it in a closed product provided the copyright notice and permission notice travel with it. This is a description of the licence text, not legal advice; if your organisation has rules about attribution in bundled output, run the licence past whoever handles that.
The practical maintenance cost is low because the surface is small. There is one source file at src/tinykeys.ts, a test directory, and a build configured through tsdown. There is no runtime dependency list to audit, only devDependencies for linting, formatting, testing and the example build. Upgrading means reading the diff of that one source file, which is a genuine advantage over a library whose behaviour is spread across plugins.
The cost that does not go away is verification on real hardware. Because matching depends on event.key and event.code, and because AltGraph reporting varies by browser and layout, a version bump is not the only thing that can change behaviour for a user. A new keyboard is enough. The demo site's key logger is the tool the README points at for that.
Editorial conclusion
Adopt tinykeys if you need a handful of keyboard shortcuts in a browser app and want a library you can read end to end. Do not adopt it if you need a documented deprecation path, a release history, or server-side key handling, none of which the README covers. Before wiring it in, verify three things yourself: that your target browsers report the key and code values your bindings assume, that the 1000ms sequence timeout suits your users, and that the binding order in your object literal puts the more specific binding first, because only the first match fires.
Frequently asked questions
What is tinykeys?
It is a small TypeScript library for binding keyboard shortcuts in the browser. You pass it a target and an object of binding strings mapped to callbacks, and it decides which callback fires for a given KeyboardEvent.
How do I install tinykeys?
The README gives one command, npm install --save tinykeys, and notes that a CDN build is available on unpkg.com. The package is ESM-first and declares engines of node >=22.
How do I use tinykeys with React?
The README recommends calling tinykeys inside a useEffect and returning the unsubscribe function it gives back, so the listener is removed on unmount. The example binds $mod+b and $mod+i this way.
What does the $mod prefix mean in tinykeys?
It is a cross-platform alias: on Mac $mod equals Meta, and on Windows and Linux it equals Control. That lets one binding string cover both platforms instead of two.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/jamiebuilds-tinykeys)Star history chart for your README
[](https://hysenlabs.com/projects/jamiebuilds-tinykeys)Was this useful?