{"name":"tinykeys","version":"4.0.0","keywords":["key","keys","bindings","hot","hotkeys","a11y","accessibility","keyboard","navigation","event","handler","react","vue","angular","ember","mousetrap","shortcuts"],"author":{"name":"Jamie Kyle","email":"me@thejameskyle.com"},"license":"MIT","_id":"tinykeys@4.0.0","maintainers":[{"name":"thejameskyle","email":"me@thejameskyle.com"}],"homepage":"https://github.com/jamiebuilds/tinykeys#readme","bugs":{"url":"https://github.com/jamiebuilds/tinykeys/issues"},"dist":{"shasum":"20f117f95248028048c628870e206f78a21c3e0b","tarball":"http://registry-npm.ywwl.com/tinykeys/download/tinykeys-4.0.0.tgz","fileCount":11,"integrity":"sha512-z5bQRQHL1PSx3lGOZEAMM2oKp0EBtn5T5f7HJnaKPOzk6vEH0wUPvdHLeQ7F4tmkek8AgoTQISUFmn/5SNF2xA==","signatures":[{"sig":"MEYCIQCsSwaZIQXAWxsdfIevesjp1+UqKfoIKDCnHQO6UF0J2wIhAI7u5hyfpsua+grHmUErDVBFoNNnJHQTn1e04E4cGoM9","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":76834,"size":14328,"noattachment":false,"key":"/tinykeys/-/tinykeys-4.0.0.tgz"},"main":"dist/tinykeys.cjs","type":"module","types":"dist/tinykeys.d.mts","unpkg":"dist/tinykeys.umd.js","module":"dist/tinykeys.mjs","source":"src/tinykeys.ts","engines":{"node":">=22"},"exports":{".":{"import":{"types":"./dist/tinykeys.d.mts","default":"./dist/tinykeys.mjs"},"require":{"types":"./dist/tinykeys.d.cts","default":"./dist/tinykeys.cjs"}}},"gitHead":"5105f7fd55f7d843eef418e91f376bef7c24b7c8","scripts":{"test":"vitest","build":"tsdown","check":"npm run -s check:types && npm run -s lint && npm run -s check:format","start":"vite example --base ./","format":"prettier --write .","precommit":"lint-staged","check:lint":"eslint .","check:types":"tsc --noEmit","check:format":"prettier --check .","build:example":"vite build ./example --base ./","prepublishOnly":"npm run -s build"},"_npmUser":{"name":"thejameskyle","email":"me@thejameskyle.com"},"overrides":{"@arethetypeswrong/core":{"fflate":"0.8.2"}},"repository":{"url":"git+https://github.com/jamiebuilds/tinykeys.git","type":"git"},"_npmVersion":"11.11.0","description":"A tiny (~1KB) & modern library for keybindings.","directories":{},"lint-staged":{"*":["prettier --write","git add"]},"sideEffects":false,"_nodeVersion":"24.14.1","_hasShrinkwrap":false,"devDependencies":{"vite":"^8.0.13","husky":"^9.1.7","eslint":"^10.4.0","tsdown":"^0.22.0","vitest":"^4.1.6","publint":"^0.3.21","prettier":"^3.8.3","@eslint/js":"^10.0.1","typescript":"^6.0.3","lint-staged":"^17.0.5","canvas-confetti":"^1.9.4","typescript-eslint":"^8.59.4","@arethetypeswrong/core":"^0.18.2","@types/canvas-confetti":"^1.9.0","@vitest/browser-playwright":"^4.1.6","prettier-plugin-tailwindcss":"^0.8.0"},"_npmOperationalInternal":{"tmp":"tmp/tinykeys_4.0.0_1779249250950_0.676900555525658","host":"s3://npm-registry-packages-npm-production"},"readme":"# `tinykeys`\n\n> A tiny (~1KB) & modern library for keybindings.\n> [See Demo](https://jamiebuilds.github.io/tinykeys/)\n\n## Install\n\n```sh\nnpm install --save tinykeys\n```\n\nOr for a CDN version, you can use it on [unpkg.com](https://unpkg.com/tinykeys)\n\n## Usage\n\n```js\nimport { tinykeys } from \"tinykeys\" // Or `window.tinykeys` using the CDN version\n\ntinykeys(window, {\n  \"Shift+D\": () => {\n    alert(\"The 'Shift' and 'd' keys were pressed at the same time\")\n  },\n  \"y e e t\": () => {\n    alert(\"The keys 'y', 'e', 'e', and 't' were pressed in order\")\n  },\n  \"$mod+([0-9])\": event => {\n    event.preventDefault()\n    alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`)\n  },\n})\n```\n\nAlternatively, if you want to only create the keybinding handler, and register\nit as an event listener yourself:\n\n```js\nimport { createKeybindingsHandler } from \"tinykeys\"\n\nlet handler = createKeybindingsHandler({\n  \"Shift+D\": () => {\n    alert(\"The 'Shift' and 'd' keys were pressed at the same time\")\n  },\n  \"y e e t\": () => {\n    alert(\"The keys 'y', 'e', 'e', and 't' were pressed in order\")\n  },\n  \"$mod+KeyD\": event => {\n    event.preventDefault()\n    alert(\"Either 'Control+d' or 'Meta+d' were pressed\")\n  },\n})\n\nwindow.addEventListener(\"keydown\", handler)\n```\n\n### React Hooks Example\n\nIf you're using tinykeys within a component, you should also make use of the\nreturned `unsubscribe()` function.\n\n```js\nimport { useEffect } from \"react\"\nimport { tinykeys } from \"tinykeys\"\n\nfunction useKeyboardShortcuts() {\n  const formatBold = useEffectEvent(() => {\n    // ...\n  })\n\n  const formatItalic = useEffectEvent(() => {\n    // ...\n  })\n\n  useEffect(() => {\n    return tinykeys(window, {\n      \"$mod+b\": formatBold,\n      \"$mod+i\": formatItalic,\n    })\n  }, [])\n}\n```\n\n## Commonly used `key`'s and `code`'s\n\nKeybindings will be matched against\n[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)\nand[`KeyboardEvent.code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values)\nwhich may have some names you don't expect.\n\n| Windows       | macOS           | `key`         | `code`                         |\n| ------------- | --------------- | ------------- | ------------------------------ |\n| N/A           | `Command` / `⌘` | `Meta`        | `MetaLeft` / `MetaRight`       |\n| `Alt`         | `Option` / `⌥`  | `Alt`         | `AltLeft` / `AltRight`         |\n| `Control`     | `Control` / `^` | `Control`     | `ControlLeft` / `ControlRight` |\n| `Shift`       | `Shift`         | `Shift`       | `ShiftLeft` / `ShiftRight`     |\n| `Space`       | `Space`         | N/A           | `Space`                        |\n| `Enter`       | `Return`        | `Enter`       | `Enter`                        |\n| `Esc`         | `Esc`           | `Escape`      | `Escape`                       |\n| `1`, `2`, etc | `1`, `2`, etc   | `1`, `2`, etc | `Digit1`, `Digit2`, etc        |\n| `a`, `b`, etc | `a`, `b`, etc   | `a`, `b`, etc | `KeyA`, `KeyB`, etc            |\n| `-`           | `-`             | `-`           | `Minus`                        |\n| `=`           | `=`             | `=`           | `Equal`                        |\n| `+`           | `+`             | `+`           | `Equal`\\*                      |\n\nSomething missing? Check out the key logger on the\n[demo website](https://jamiebuilds.github.io/tinykeys/)\n\n> _\\* Some keys will have the same code as others because they appear on the\n> same key on the keyboard. Keep in mind how this is affected by international\n> keyboards which may have different layouts._\n\n## Key aliases\n\nIn some instances, tinykeys will alias keys depending on the platform to\nsimplify cross-platform keybindings on international keyboards.\n\n### `AltGraph` (modifier)\n\nOn Windows, on many non-US standard keyboard layouts, there is a key named\n`Alt Gr` or `AltGraph` in the browser, in some browsers, pressing `Control+Alt`\nwill report `AltGraph` as being pressed instead.\n\nSimilarly on macOS, the `Alt` (`Option`) key will sometimes be reported as the\n`AltGraph` key.\n\n**Note:** The purpose of the `Alt Gr` key is to type \"Alternate Graphics\" so you\nwill often want to use the `event.code` (`KeyS`) for letters instead of\n`event.key` (`S`)\n\n```js\ntinykeys(window, {\n  \"Control+Alt+KeyS\": event => {\n    // macOS: `Control+Alt+S` or `Control+AltGraph+S`\n    // Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S`\n  },\n  \"$mod+Alt+KeyS\": event => {\n    // macOS: `Meta+Alt+S` or `Meta+AltGraph+S`\n    // Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S`\n  },\n})\n```\n\n## Keybinding Syntax\n\nKeybindings are made up of a **_sequence_** of **_presses_**.\n\nA **_press_** can be as simple as a single **_key_** which matches against\n[`KeyboardEvent.code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values)\nand\n[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)\n(case-insensitive).\n\n```js\n// Matches `event.key`:\n\"d\"\n// Matches: `event.code`:\n\"KeyD\"\n```\n\nPresses can optionally be prefixed with **_modifiers_** which match against any\nvalid value to\n[`KeyboardEvent.getModifierState()`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState).\n\n```js\n\"Control+d\"\n\"Meta+d\"\n\"Shift+D\"\n\"Alt+KeyD\"\n\"Meta+Shift+D\"\n```\n\nModifiers can be made optional by wrapping them with `[brackets]`\n\n```js\n\"[Shift]+?\"\n\"Control+[Shift]+D\"\n```\n\nThere is also a special `$mod` modifier that makes it easy to support cross\nplatform keybindings:\n\n- Mac: `$mod` = `Meta` (⌘)\n- Windows/Linux: `$mod` = `Control`\n\n```js\n\"$mod+D\" // Meta/Control+D\n\"$mod+Shift+D\" // Meta/Control+Shift+D\n```\n\nYou can also use parenthesis to use case-insensitive regular expressions to\nmatch multiple keys (Note: This does not work for modifiers).\n\n```js\n\"$mod+([0-9])\" // $mod+0, $mod+1, $mod+2, etc...\n// equivalent regex: /^(?:[0-9])$/iv\n```\n\n### Keybinding Sequences\n\nKeybindings can also consist of several key presses in a row:\n\n```js\n\"g i\" // i.e. \"Go to Inbox\"\n\"g a\" // i.e. \"Go to Archive\"\n\"ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight B A\"\n```\n\nEach press can optionally be prefixed with modifier keys:\n\n```js\n\"$mod+K $mod+1\" // i.e. \"Toggle Level 1\"\n\"$mod+K $mod+2\" // i.e. \"Toggle Level 2\"\n\"$mod+K $mod+3\" // i.e. \"Toggle Level 3\"\n```\n\nEach press in the sequence must be pressed within 1000ms of the last.\n\n#### Keybinding Priorities\n\nIt should rarely come up, but the order of your keybindings matters. So if you\nwere to create two keybindings that would match the same event, only the first\nwill fire.\n\nThis mainly comes up when you create bindings for both `KeyboardEvent#key` and\n`KeyboardEvent#code` in order to support more keyboard layouts:\n\n```js\ntinykeys(window, {\n  \"$mod+b\": () => console.log(\"make bold\"),\n  \"$mod+KeyB\": () => console.log(\"also bold\"),\n})\n```\n\n> In this case, if the user holds `$mod` and types `b (KeyB)` it will only\n> trigger `$mod+B` and log `\"make bold\"`\n\nThis can also come up when you have sequences that overlap with other\nkeybindings.\n\n<!-- prettier-ignore -->\n```js\ntinykeys(window, {\n  \"g a\": () => console.log(\"goto archive\"),\n  \"a\": () => console.log(\"archive item\"),\n})\n```\n\n> In this case, if the user types `g a` it will only log `\"goto archive\"`\n\nHowever, you can also break later keybindings by declaring earlier keybindings\nthat will always win:\n\n<!-- prettier-ignore -->\n```js\ntinykeys(window, {\n  \"g\": () => console.log(\"show goto indicator\"),\n  \"g a\": () => console.log(\"goto archive\"),\n})\n```\n\n> In this case, if the user types `g a` it will only log `\"show goto indicator\"`\n> (after the first `g`)\n\n#### Keybinding Sequence Conflicts\n\nIn some circumstances, overlapping keybindings can cause \"conflicts\" where a\nkeybinding has been completely typed, but there is another keybinding declared\nearlier that might still match.\n\n```js\ntinykeys(window, {\n  \"a b c\": () => console.log(\"abc\"),\n  \"a b\": () => console.log(\"ab\"),\n})\n```\n\n> In this case, if the user types `a b` it will not trigger either keybinding,\n> but it will print this warning to the console:\n>\n> ```\n> warning: tinykeys: Conflict found, \"a b\" did not fire, waiting for: [\"a b c\"]\n> ```\n\n### Display the keyboard sequence\n\nYou can use the `parseKeybinding` method to get a structured representation of a\nkeyboard shortcut. It can be useful when you want to display it in a fancier way\nthan a plain string.\n\n```js\nimport { parseKeybinding } from \"tinykeys\"\n\nlet parsedShortcut = parseKeybinding(\"$mod+Shift+K $mod+1\")\n```\n\nResults into:\n\n<!-- prettier-ignore -->\n```js\n[\n  [[\"Meta\", \"Shift\"], \"K\"],\n  [[\"Meta\"], \"1\"],\n]\n```\n\n## Additional Configuration Options\n\nYou can configure the behavior of tinykeys in a couple ways using a third\n`options` parameter.\n\n```js\ntinykeys(\n  window,\n  {\n    M: toggleMute,\n  },\n  {\n    event: \"keyup\",\n    capture: true,\n  },\n)\n```\n\n### `options.event`\n\nValid values: `\"keydown\"`, `\"keyup\"`\n\nKey presses will listen to this event (default: `\"keydown\"`).\n\n> **Note:** Do not pass `\"keypress\"`, it is deprecated in browsers.\n\n### `options.timeout`\n\nKeybinding sequences will wait this long between key presses before cancelling\n(default: `1000`).\n\n> **Note:** Setting this value too low (i.e. `300`) will be too fast for many of\n> your users.\n\n### `options.ignore`\n\nAdd a filter for which keyboard events should be ignored.\n\nBy default, tinykeys will ignore keyboard events from `[contenteditable]`,\n`input`, `textarea`, and `select` unless they are the `event.currentTarget`.\n","publish_time":1779249251100,"_cnpm_publish_time":1779249251100,"dist-tags":{"latest":"4.0.1"}}