{"id":13397993,"url":"https://github.com/jaywcjlove/hotkeys-js","last_synced_at":"2026-02-25T11:16:47.459Z","repository":{"id":32258760,"uuid":"35833177","full_name":"jaywcjlove/hotkeys-js","owner":"jaywcjlove","description":"➷ A robust Javascript library for capturing keyboard input. It has no dependencies. ","archived":false,"fork":false,"pushed_at":"2025-08-21T05:48:13.000Z","size":54619,"stargazers_count":7044,"open_issues_count":150,"forks_count":419,"subscribers_count":72,"default_branch":"master","last_synced_at":"2025-11-12T05:20:26.214Z","etag":null,"topics":["browser","hotkey","hotkeys","javascript","keyboard","keyboard-shortcuts","keymaster","keypress","masterkey","shortcut","shortcutkey","shortcuts"],"latest_commit_sha":null,"homepage":"https://jaywcjlove.github.io/hotkeys-js","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dvf/blockchain","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaywcjlove.png","metadata":{"files":{"readme":"README-zh.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"ko_fi":"jaywcjlove","buy_me_a_coffee":"jaywcjlove","custom":["https://www.paypal.me/kennyiseeyou","https://jaywcjlove.github.io/#/sponsor"]}},"created_at":"2015-05-18T17:49:52.000Z","updated_at":"2025-11-11T21:26:17.000Z","dependencies_parsed_at":"2023-07-20T21:24:32.915Z","dependency_job_id":"1e069cf0-97b4-4813-8e2c-6e65719c208c","html_url":"https://github.com/jaywcjlove/hotkeys-js","commit_stats":{"total_commits":523,"total_committers":56,"mean_commits":9.339285714285714,"dds":"0.22562141491395793","last_synced_commit":"b0038773f3b902574f22af747f3bb003a850f1da"},"previous_names":["jaywcjlove/hotkeys"],"tags_count":107,"template":false,"template_full_name":null,"purl":"pkg:github/jaywcjlove/hotkeys-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaywcjlove%2Fhotkeys-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaywcjlove%2Fhotkeys-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaywcjlove%2Fhotkeys-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaywcjlove%2Fhotkeys-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaywcjlove","download_url":"https://codeload.github.com/jaywcjlove/hotkeys-js/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaywcjlove%2Fhotkeys-js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":285898425,"owners_count":27250309,"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","status":"online","status_checked_at":"2025-11-23T02:00:06.149Z","response_time":135,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["browser","hotkey","hotkeys","javascript","keyboard","keyboard-shortcuts","keymaster","keypress","masterkey","shortcut","shortcutkey","shortcuts"],"created_at":"2024-07-30T18:02:00.552Z","updated_at":"2026-02-25T11:16:47.453Z","avatar_url":"https://github.com/jaywcjlove.png","language":"JavaScript","funding_links":["https://ko-fi.com/jaywcjlove","https://buymeacoffee.com/jaywcjlove","https://www.paypal.me/kennyiseeyou","https://jaywcjlove.github.io/#/sponsor"],"categories":["JavaScript","TypeScript","🔧 Utilities \u0026 Miscellaneous"],"sub_categories":[],"readme":"\u003cdiv markdown=\"1\"\u003e\n  \u003csup\u003e使用\u003ca href=\"https://wangchujiang.com/#/app\" target=\"_blank\"\u003e我的应用\u003c/a\u003e也是一种\u003ca href=\"https://wangchujiang.com/#/sponsor\" target=\"_blank\"\u003e支持\u003c/a\u003e我的方式：\u003c/sup\u003e\n  \u003cbr\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6758053530\" title=\"Scap: Screenshot \u0026 Markup Edit for macOS\"\u003e\u003cimg alt=\"Scap: Screenshot \u0026 Markup Edit\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/scap.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6757317079\" title=\"Screen Test for macOS\"\u003e\u003cimg alt=\"Screen Test\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/screen-test.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/Deskmark/6755948110\" title=\"Deskmark for macOS\"\u003e\u003cimg alt=\"Deskmark\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/deskmark.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/Keyzer/6500434773\" title=\"Keyzer for macOS\"\u003e\u003cimg alt=\"Keyzer\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/keyzer.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://github.com/jaywcjlove/vidwall-hub\" title=\"Vidwall Hub for macOS\"\u003e\u003cimg alt=\"Vidwall Hub\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/vidwall-hub.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/VidCrop/6752624705\" title=\"VidCrop for macOS\"\u003e\u003cimg alt=\"VidCrop\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/vidcrop.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/Vidwall/6747587746\" title=\"Vidwall for macOS\"\u003e\u003cimg alt=\"Vidwall\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/vidwall.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://wangchujiang.com/mousio-hint/\" title=\"Mousio Hint for macOS\"\u003e\u003cimg alt=\"Mousio Hint\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/mousio-hint.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6746747327\" title=\"Mousio for macOS\"\u003e\u003cimg alt=\"Mousio\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/mousio.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6745227444\" title=\"Musicer for macOS\"\u003e\u003cimg alt=\"Musicer\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/musicer.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6743841447\" title=\"Audioer for macOS\"\u003e\u003cimg alt=\"Audioer\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/audioer.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6744690194\" title=\"FileSentinel for macOS\"\u003e\u003cimg alt=\"FileSentinel\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/file-sentinel.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6743495172\" title=\"FocusCursor for macOS\"\u003e\u003cimg alt=\"FocusCursor\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/focus-cursor.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6742680573\" title=\"Videoer for macOS\"\u003e\u003cimg alt=\"Videoer\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/videoer.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6740425504\" title=\"KeyClicker for macOS\"\u003e\u003cimg alt=\"KeyClicker\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/key-clicker.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6739052447\" title=\"DayBar for macOS\"\u003e\u003cimg alt=\"DayBar\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/daybar.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6739444407\" title=\"Iconed for macOS\"\u003e\u003cimg alt=\"Iconed\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/iconed.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6737160756\" title=\"Mousio for macOS\"\u003e\u003cimg alt=\"Mousio\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/rightmenu-master.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6723903021\" title=\"Paste Quick for macOS\"\u003e\u003cimg alt=\"Quick RSS\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/paste-quick.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6670696072\" title=\"Quick RSS for macOS/iOS\"\u003e\u003cimg alt=\"Quick RSS\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/quick-rss.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6670167443\" title=\"Web Serve for macOS\"\u003e\u003cimg alt=\"Web Serve\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/web-serve.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6503953628\" title=\"Copybook Generator for macOS/iOS\"\u003e\u003cimg alt=\"Copybook Generator\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/copybook-generator.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6471227008\" title=\"DevTutor for macOS/iOS\"\u003e\u003cimg alt=\"DevTutor for SwiftUI\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/devtutor.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6479819388\" title=\"RegexMate for macOS/iOS\"\u003e\u003cimg alt=\"RegexMate\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/regex-mate.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6479194014\" title=\"Time Passage for macOS/iOS\"\u003e\u003cimg alt=\"Time Passage\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/time-passage.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6478772538\" title=\"IconizeFolder for macOS\"\u003e\u003cimg alt=\"Iconize Folder\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/iconize-folder.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6478511402\" title=\"Textsound Saver for macOS/iOS\"\u003e\u003cimg alt=\"Textsound Saver\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/textsound-saver.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6476924627\" title=\"Create Custom Symbols for macOS\"\u003e\u003cimg alt=\"Create Custom Symbols\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/create-custom-symbols.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6476452351\" title=\"DevHub for macOS\"\u003e\u003cimg alt=\"DevHub\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/devhub.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6476400184\" title=\"Resume Revise for macOS\"\u003e\u003cimg alt=\"Resume Revise\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/resume-revise.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6472593276\" title=\"Palette Genius for macOS\"\u003e\u003cimg alt=\"Palette Genius\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/palette-genius.png\"\u003e\u003c/a\u003e\n  \u003ca target=\"_blank\" href=\"https://apps.apple.com/app/6470879005\" title=\"Symbol Scribe for macOS\"\u003e\u003cimg alt=\"Symbol Scribe\" height=\"52\" width=\"52\" src=\"https://wangchujiang.com/appicon/symbol-scribe.png\"\u003e\u003c/a\u003e\n\u003c/div\u003e\n\u003chr\u003e\n\n# Hotkeys\n\n\u003c!--dividing--\u003e\n\n[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)\n[![](https://img.shields.io/npm/dm/hotkeys-js?logo=npm)](https://www.npmjs.com/package/hotkeys-js)\n[![](https://img.shields.io/github/stars/jaywcjlove/hotkeys-js.svg)](https://github.com/jaywcjlove/hotkeys/stargazers)\n![no dependencies](http://jaywcjlove.github.io/sb/status/no-dependencies.svg)\n[![GitHub Actions CI](https://github.com/jaywcjlove/hotkeys-js/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/hotkeys-js/actions/workflows/ci.yml)\n[![Coverage Status](https://jaywcjlove.github.io/hotkeys-js/coverage.svg)](https://jaywcjlove.github.io/hotkeys-js/lcov-report/index.html)\n[![English](https://jaywcjlove.github.io/sb/lang/english.svg)](https://jaywcjlove.github.io/hotkeys-js/)\n[![jaywcjlove/hotkeys-js](https://jaywcjlove.github.io/sb/ico/gitee.svg)](https://gitee.com/jaywcjlove/hotkeys)\n\nHotKeys.js 是一个具有一些非常特殊功能的输入捕获库，它易于上手和使用，占用空间合理（[~6kB](https://bundlephobia.com/result?p=hotkeys-js)）（压缩后：**`2.8kB`**），无依赖。它不应该干扰任何 JavaScript 库或框架。官方文档 [演示预览](https://jaywcjlove.github.io/hotkeys-js)。[更多示例](https://github.com/jaywcjlove/hotkeys-js/issues?q=label%3ADemo+)。\n\n```bash\n╭┈┈╮          ╭┈┈╮  ╭┈┈╮\n┆  ├┈┈..┈┈┈┈┈.┆  └┈╮┆  ├┈┈..┈┈┈┈┈..┈┈.┈┈..┈┈┈┈┈.\n┆     ┆┆  □  ┆┆   ┈┤┆    \u003c ┆  -__┘┆  ┆  ┆┆__ ┈┈┤\n╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈╯╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈  ┆╰┈┈┈┈┈╯\n                                  ╰┈┈┈┈┈╯\n```\n\n## 使用\n\n您的系统需要安装 `Node.js`。\n\n```bash\nnpm install hotkeys-js --save\n```\n\n```js\nimport hotkeys from 'hotkeys-js';\n\nhotkeys('f5', function(event, handler){\n  // 阻止 WINDOWS 系统下的默认刷新事件\n  event.preventDefault()\n  alert('你按下了 F5!')\n});\n```\n\n### 浏览器使用\n\n或者手动下载并在 HTML 中链接 **hotkeys.js**。该库提供了不同格式以满足不同的使用需求：\n\n**CDN 链接：** [UNPKG](https://unpkg.com/hotkeys-js/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/hotkeys-js/) | [Githack](https://raw.githack.com/jaywcjlove/hotkeys/master/dist/) | [Statically](https://cdn.statically.io/gh/jaywcjlove/hotkeys/master/dist/)\n\n**可用格式：**\n\n**IIFE（立即调用函数表达式）- 推荐用于直接浏览器使用：**\n\n```html\n\u003cscript src=\"https://unpkg.com/hotkeys-js/dist/hotkeys-js.min.js\"\u003e\n\u003c/script\u003e\n\u003cscript type=\"text/javascript\"\u003e\nhotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){\n  switch (handler.key) {\n    case 'ctrl+a': alert('你按下了 ctrl+a!');\n      break;\n    case 'ctrl+b': alert('你按下了 ctrl+b!');\n      break;\n    case 'r': alert('你按下了 r!');\n      break;\n    case 'f': alert('你按下了 f!');\n      break;\n    default: alert(event);\n  }\n});\n\u003c/script\u003e\n```\n\n**UMD（通用模块定义）- 用于 CommonJS/AMD 环境：**\n\n```html\n\u003cscript src=\"https://unpkg.com/hotkeys-js/dist/hotkeys-js.umd.cjs\"\u003e\n\u003c/script\u003e\n```\n\n**ES 模块 - 用于支持模块的现代浏览器：**\n\n```html\n\u003cscript type=\"module\"\u003e\nimport hotkeys from 'https://unpkg.com/hotkeys-js/dist/hotkeys-js.js';\nhotkeys('ctrl+a', function(event, handler){\n  alert('你按下了 ctrl+a!');\n});\n\u003c/script\u003e\n```\n\n### 在 React 中使用\n\n[react-hotkeys](https://github.com/jaywcjlove/react-hotkeys) 是监听 keydown 和 keyup 键盘事件的 React 组件，定义和调度键盘快捷键。详细的使用方法请查看其文档 [react-hotkeys](https://github.com/jaywcjlove/react-hotkeys)。  \n[react-hotkeys-hook](https://github.com/JohannesKlauss/react-hotkeys-hook) - 在组件中使用键盘快捷键的 React hook。请确保您至少安装了 react 和 react-dom 的 16.8 版本，否则 hooks 将不会为您工作。\n\n## 浏览器支持\n\nHotkeys.js 已经过测试，应该在以下浏览器中工作。\n\n```shell\nInternet Explorer 6+\nSafari\nFirefox\nChrome\n```\n\n## 支持的按键\n\nHotKeys 理解以下修饰符：`⇧`、`shift`、`option`、`⌥`、`alt`、`ctrl`、`control`、`command` 和 `⌘`。\n\n以下特殊按键可用于快捷键：backspace、tab、clear、enter、return、esc、escape、space、up、down、left、right、home、end、pageup、pagedown、del、delete、f1 到 f19、num_0 到 num_9、num_multiply、num_add、num_enter、num_subtract、num_decimal、num_divide。\n\n`⌘` Command()  \n`⌃` Control  \n`⌥` Option(alt)  \n`⇧` Shift  \n`⇪` Caps Lock(Capital)  \n~~`fn` 不支持 fn~~  \n`↩︎` return/Enter space  \n\n## 定义快捷键\n\n暴露了一个全局方法，当直接调用时定义快捷键。\n\n```ts\ndeclare interface HotkeysInterface extends HotkeysAPI {\n  (key: string, method: KeyHandler): void;\n  (key: string, scope: string, method: KeyHandler): void;\n  (key: string, option: HotkeysOptions, method: KeyHandler): void;\n  shift?: boolean;\n  ctrl?: boolean;\n  alt?: boolean;\n  option?: boolean;\n  control?: boolean;\n  cmd?: boolean;\n  command?: boolean;\n}\ndeclare interface HotkeysAPI {\n  setScope: SetScope;\n  getScope: GetScope;\n  deleteScope: DeleteScope;\n  getPressedKeyCodes: GetPressedKeyCodes;\n  getPressedKeyString: GetPressedKeyString;\n  getAllKeyCodes: GetAllKeyCodes;\n  isPressed: IsPressed;\n  filter: Filter;\n  trigger: Trigger;\n  unbind: Unbind;\n  noConflict: NoConflict;\n  keyMap: Record\u003cstring, number\u003e;\n  modifier: Record\u003cstring, number\u003e;\n  modifierMap: Record\u003cstring | number, number | string\u003e;\n}\n```\n\n```js\nhotkeys('f5', function(event, handler) {\n  // 阻止 WINDOWS 系统下的默认刷新事件\n  event.preventDefault();\n  alert('你按下了 F5!');\n});\n\n// 返回 false 停止事件并阻止默认浏览器事件\n// Mac OS 系统将 `command + r` 定义为刷新快捷键\nhotkeys('ctrl+r, command+r', function() {\n  alert('停止刷新!');\n  return false;\n});\n\n// 单个按键\nhotkeys('a', function(event,handler){\n  //event.srcElement: input\n  //event.target: input\n  if(event.target === \"input\"){\n      alert('你按下了 a!')\n  }\n  alert('你按下了 a!')\n});\n\n// 组合键\nhotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){\n  switch (handler.key) {\n    case 'ctrl+a': alert('你按下了 ctrl+a!');\n      break;\n    case 'ctrl+b': alert('你按下了 ctrl+b!');\n      break;\n    case 'r': alert('你按下了 r!');\n      break;\n    case 'f': alert('你按下了 f!');\n      break;\n    default: alert(event);\n  }\n});\n\nhotkeys('ctrl+a+s', function() {\n    alert('你按下了 ctrl+a+s!');\n});\n\n// 使用作用域\nhotkeys('*','wcj', function(event){\n  console.log('做一些事情', event);\n});\n```\n\n#### option 选项\n\n- `scope\u003cString\u003e`：设置快捷键生效的作用域\n- `element\u003cHTMLElement\u003e`：指定要绑定事件的 DOM 元素\n- `keyup\u003cBoolean\u003e`：是否在按键释放时触发快捷键\n- `keydown\u003cBoolean\u003e`：是否在按键按下时触发快捷键\n- `splitKey\u003cString\u003e`：组合键的分隔符（默认为 `+`）\n- `capture\u003cBoolean\u003e`：是否在捕获阶段触发监听器（在事件冒泡之前）\n- `single\u003cBoolean\u003e`：只允许一个回调函数（自动解绑之前的）\n\n```js\nhotkeys('o, enter', {\n  scope: 'wcj',\n  element: document.getElementById('wrapper'),\n}, function() {\n  console.log('做其他事情');\n});\n\nhotkeys('ctrl-+', { splitKey: '-' }, function(e) {\n  console.log('你按下了 ctrl 和 +');\n});\n\nhotkeys('+', { splitKey: '-' }, function(e){\n  console.log('你按下了 +');\n})\n```\n\n**keyup**\n\n**按键按下** 和 **按键释放** 都执行回调事件。\n\n```js\nhotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) {\n  if (event.type === 'keydown') {\n    console.log('keydown:', event.type, handler, handler.key);\n  }\n\n  if (event.type === 'keyup') {\n    console.log('keyup:', event.type, handler, handler.key);\n  }\n});\n```\n\n## API 参考\n\n星号 \"*\"\n\n修饰键判断\n\n```js\nhotkeys('*', function() {\n  if (hotkeys.shift) {\n    console.log('按下了 shift!');\n  }\n\n  if (hotkeys.ctrl) {\n    console.log('按下了 ctrl!');\n  }\n\n  if (hotkeys.alt) {\n    console.log('按下了 alt!');\n  }\n\n  if (hotkeys.option) {\n    console.log('按下了 option!');\n  }\n\n  if (hotkeys.control) {\n    console.log('按下了 control!');\n  }\n\n  if (hotkeys.cmd) {\n    console.log('按下了 cmd!');\n  }\n\n  if (hotkeys.command) {\n    console.log('按下了 command!');\n  }\n});\n```\n\n### setScope\n\n使用 `hotkeys.setScope` 方法来设置作用域。除了 'all' 之外，只能有一个活动作用域。默认情况下 'all' 总是活动的。\n\n```js\n// 定义带有作用域的快捷键\nhotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {\n  console.log('做一些事情');\n});\nhotkeys('o, enter', 'files', function() {\n  console.log('做其他事情');\n});\n\n// 设置作用域（只有 'all' 和 'issues' 快捷键会被处理）\nhotkeys.setScope('issues'); // 默认作用域是 'all'\n```\n\n### getScope\n\n使用 `hotkeys.getScope` 方法来获取作用域。\n\n```js\nhotkeys.getScope();\n```\n\n### deleteScope\n\n使用 `hotkeys.deleteScope` 方法来删除作用域。这也会移除与之关联的所有热键。\n\n```js\nhotkeys.deleteScope('issues');\n```\n如果需要在删除后设置新的作用域，可以使用第二个参数。\n\n```js\nhotkeys.deleteScope('issues', 'newScopeName');\n```\n\n### unbind\n\n与定义快捷键类似，它们可以使用 `hotkeys.unbind` 来解绑。\n\n```js\n// 解绑 'a' 处理器\nhotkeys.unbind('a');\n\n// 只为单个作用域解绑热键\n// 如果没有指定作用域，默认为当前作用域\n// （hotkeys.getScope()）\nhotkeys.unbind('o, enter', 'issues');\nhotkeys.unbind('o, enter', 'files');\n```\n\n通过函数解绑事件。\n\n```js\nfunction example() {\n  hotkeys('a', example);\n  hotkeys.unbind('a', example);\n\n  hotkeys('a', 'issues', example);\n  hotkeys.unbind('a', 'issues', example);\n}\n```\n\n解绑所有。\n\n```js\nhotkeys.unbind();\n```\n\n### isPressed\n\n例如，如果当前按下了 `M` 键，`hotkeys.isPressed(77)` 返回 true。\n\n```js\nhotkeys('a', function() {\n  console.log(hotkeys.isPressed('a')); //=\u003e true\n  console.log(hotkeys.isPressed('A')); //=\u003e true\n  console.log(hotkeys.isPressed(65)); //=\u003e true\n});\n```\n\n### trigger\n\n触发快捷键事件\n\n```js\nhotkeys.trigger('ctrl+o');\nhotkeys.trigger('ctrl+o', 'scope2');\n```\n\n### getPressedKeyCodes\n\n返回当前按下的键码数组。\n\n```js\nhotkeys('command+ctrl+shift+a,f', function() {\n  console.log(hotkeys.getPressedKeyCodes()); //=\u003e [17, 65] 或 [70]\n})\n```\n\n### getPressedKeyString\n\n返回当前按下的键字符串数组。\n\n```js\nhotkeys('command+ctrl+shift+a,f', function() {\n  console.log(hotkeys.getPressedKeyString()); \n  //=\u003e ['⌘', '⌃', '⇧', 'A', 'F']\n})\n```\n\n### getAllKeyCodes\n\n获取所有注册码的列表。\n\n```js\nhotkeys('command+ctrl+shift+a,f', function() {\n  console.log(hotkeys.getAllKeyCodes());\n  // [\n  //   { \n  //      scope: 'all', \n  //      shortcut: 'command+ctrl+shift+a', \n  //      mods: [91, 17, 16], \n  //      keys: [91, 17, 16, 65] \n  //    },\n  //   { scope: 'all', shortcut: 'f', mods: [], keys: [42] }\n  // ]\n})\n```\n\n### filter\n\n默认情况下，`INPUT` `SELECT` `TEXTAREA` 元素不启用热键。`Hotkeys.filter` 返回 `true` 快捷键设置发挥作用，`false` 快捷键设置失败。\n\n```js\nhotkeys.filter = function(event){\n  return true;\n}\n// 如何为编辑标签添加过滤器。\n// \u003cdiv contentEditable=\"true\"\u003e\u003c/div\u003e\n// \"contentEditable\" 不支持的较旧浏览器会被丢弃\nhotkeys.filter = function(event) {\n  var target = event.target || event.srcElement;\n  var tagName = target.tagName;\n  return !(\n    target.isContentEditable ||\n    tagName == 'INPUT' ||\n    tagName == 'SELECT' ||\n    tagName == 'TEXTAREA'\n  );\n}\n\nhotkeys.filter = function(event){\n  var tagName = (event.target || event.srcElement).tagName;\n  hotkeys.setScope(\n    /^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other'\n  );\n  return true;\n}\n```\n\n### noConflict\n\n放弃 HotKeys 对 `hotkeys` 变量的控制。\n\n```js\nvar k = hotkeys.noConflict();\nk('a', function() {\n  console.log(\"做一些事情\")\n});\n\nhotkeys()\n// --\u003eUncaught TypeError: hotkeys is not a function(anonymous function)\n// @ VM2170:2InjectedScript._evaluateOn\n// @ VM2165:883InjectedScript._evaluateAndWrap\n// @ VM2165:816InjectedScript.evaluate @ VM2165:682\n```\n\n## 开发\n\n要开发，需要安装依赖，获取代码：\n\n```shell\n$ git https://github.com/jaywcjlove/hotkeys.git\n$ cd hotkeys     # 进入目录\n$ npm install    # 或者 yarn install\n```\n\n要开发，运行自重载构建：\n\n```shell\n$ npm run watch\n```\n\n运行文档网站环境。\n\n```shell\n$ npm run doc # 生成文档网页\n# 实时生成文档网页\n$ npm run start \n```\n\n要贡献，请 fork Hotkeys.js，添加您的补丁和测试（在 `test/` 文件夹中）并提交拉取请求。\n\n```shell\n$ npm run test\n$ npm run test:watch # 开发模式\n```\n\n## 贡献者\n\n一如既往，感谢我们出色的贡献者！\n\n\u003ca href=\"https://github.com/jaywcjlove/hotkeys-js/graphs/contributors\"\u003e\n  \u003cimg src=\"https://jaywcjlove.github.io/hotkeys-js/CONTRIBUTORS.svg\" /\u003e\n\u003c/a\u003e\n\n使用 [action-contributors](https://github.com/jaywcjlove/github-action-contributors) 制作。\n\n特别感谢 [@dimensi](https://github.com/dimensi) 对版本 [4.0](https://github.com/jaywcjlove/hotkeys-js/issues/313) 的重构。\n\n## 许可证\n\n[MIT © Kenny Wong](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaywcjlove%2Fhotkeys-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaywcjlove%2Fhotkeys-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaywcjlove%2Fhotkeys-js/lists"}