{"id":28358936,"url":"https://github.com/violentmonkey/vm-shortcut","last_synced_at":"2025-09-01T01:04:37.739Z","repository":{"id":44643257,"uuid":"129595214","full_name":"violentmonkey/vm-shortcut","owner":"violentmonkey","description":"Register a shortcut for a function","archived":false,"fork":false,"pushed_at":"2024-03-19T04:18:43.000Z","size":301,"stargazers_count":49,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-04T09:42:33.936Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://violentmonkey.github.io/vm-shortcut/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/violentmonkey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2018-04-15T09:26:11.000Z","updated_at":"2025-05-10T22:54:48.000Z","dependencies_parsed_at":"2023-02-09T04:01:28.771Z","dependency_job_id":"c19df9cc-6754-49a0-b414-49b573766bb8","html_url":"https://github.com/violentmonkey/vm-shortcut","commit_stats":{"total_commits":89,"total_committers":5,"mean_commits":17.8,"dds":0.5955056179775281,"last_synced_commit":"a73ae9259733f42b731de9f182258576f305d777"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/violentmonkey/vm-shortcut","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/violentmonkey%2Fvm-shortcut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/violentmonkey%2Fvm-shortcut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/violentmonkey%2Fvm-shortcut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/violentmonkey%2Fvm-shortcut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/violentmonkey","download_url":"https://codeload.github.com/violentmonkey/vm-shortcut/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/violentmonkey%2Fvm-shortcut/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260673826,"owners_count":23045006,"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","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":[],"created_at":"2025-05-28T08:37:10.616Z","updated_at":"2025-09-01T01:04:37.729Z","avatar_url":"https://github.com/violentmonkey.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# VM.shortcut\n\n[![NPM](https://img.shields.io/npm/v/@violentmonkey/shortcut.svg)](https://npm.im/@violentmonkey/shortcut)\n![License](https://img.shields.io/npm/l/@violentmonkey/shortcut.svg)\n[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@violentmonkey/shortcut)\n\nRegister a shortcut for a function.\n\nThis is a helper script for Violentmonkey.\n\n👉 Playground: https://violentmonkey.github.io/vm-shortcut/\n\n## Usage\n\n### Importing\n\n1. Use in a userscript:\n\n   ```js\n   // ...\n   // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1\n   // ...\n\n   const { register, ... } = VM.shortcut;\n   ```\n\n1. Use as a module:\n\n   ```bash\n   $ yarn add @violentmonkey/shortcut\n   ```\n\n   ```js\n   import { register, ... } from '@violentmonkey/shortcut';\n   ```\n\n### Registering Shortcuts\n\n1. Register a shortcut:\n\n   ```js\n   import { register } from '@violentmonkey/shortcut';\n\n   register('c-i', () =\u003e {\n     console.log('You just pressed Ctrl-I');\n   });\n\n   // shortcuts will be enabled by default\n   ```\n\n1. Enable or disable all shortcuts:\n\n   ```js\n   import { enable, disable } from '@violentmonkey/shortcut';\n\n   disable();\n   // ...\n   enable();\n   ```\n\n1. Key sequences:\n\n   ```js\n   import { register } from '@violentmonkey/shortcut';\n\n   register('c-a c-b', () =\u003e {\n     console.log('You just pressed Ctrl-A Ctrl-B sequence');\n   });\n   ```\n\n1. Handle keys with custom listeners (e.g. use with text editor like TinyMCE):\n\n   ```js\n   import { handleKey } from '@violentmonkey/shortcut';\n\n   function onKeyDown(e) {\n     handleKey(e);\n   }\n\n   addMyKeyDownListener(onKeyDown);\n   ```\n\n### Advanced Usage\n\nThe usage above is with the default keyboard service. However you can use the `KeyboardService` directly to get full control of the class:\n\n```js\nimport { KeyboardService } from '@violentmonkey/shortcut';\n\nconst service = new KeyboardService();\n// Or pass options\nconst service = new KeyboardService({\n  sequenceTimeout: 500,\n});\n\nservice.enable();\n\nservice.register('c-i', () =\u003e {\n  console.log('You just pressed Ctrl-I');\n});\n\n// Only register the following key when `disableThisKey` is false\nservice.register(\n  'g g',\n  () =\u003e {\n    console.log('Now disableThisKey is false and you pressed `g g`');\n  },\n  {\n    condition: '!disableThisKey',\n  }\n);\n\n// Update `disableThisKey` on different conditions\n// Note: these callbacks are just demos, you need to implement them by yourself!!!\nonOneCondition(() =\u003e {\n  service.setContext('disableThisKey', true);\n});\nonAnotherCondition(() =\u003e {\n  service.setContext('disableThisKey', false);\n});\n\n// Disable the shortcuts and unbind all events whereever you want\nservice.disable();\n\n// Reenable the shortcuts later\nservice.enable();\n```\n\n## API\n\n[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@violentmonkey/shortcut)\n\n## Key Definition\n\nA key sequence is a space-separated list of combined keys. Each combined key is composed of zero or more modifiers and exactly one base key in the end, concatenated with dashes (`-`). The modifiers are always case-insensitive and can be abbreviated as their first letters.\n\nHere are some valid examples:\n\n```\nctrl-alt-c\nctrl-a-c\nc-a-c\n```\n\nPossible modifiers are:\n\n- `c`, `ctrl`, `control`\n- `s`, `shift`\n- `a`, `alt`\n- `m`, `meta`, `cmd`\n- `cm`, `ctrlcmd`\n\nThere is one special case, `ctrlcmd` for `ctrl` on Windows and `cmd` for macOS, so if we register `ctrlcmd-s` to save something, the callback will be called when `ctrl-s` is pressed on Windows, and when `cmd-s` is pressed on macOS. This is useful to register cross-platform shortcuts.\n\n## Condition Syntax\n\n- `conditionA` - when `conditionA` is truthy\n- `!conditionB` - when `conditionB` is falsy\n- `conditionA \u0026\u0026 conditionB` - when both `conditionA` and `conditionB` are truthy\n\nFor more complicated cases, it's recommended to handle the logic in a function and store the result as a simple condition to the context.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviolentmonkey%2Fvm-shortcut","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviolentmonkey%2Fvm-shortcut","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviolentmonkey%2Fvm-shortcut/lists"}