It is a common case to provide a keyboard shortcut for a function in a userscript.
We provided an easy way to achieve this by introducing @violentmonkey/shortcut.
Note: @violentmonkey/shortcut is a library provided by the Violentmonkey team, but it is just JavaScript and can be used with other script managers as well. You can find the API reference in its README page.
Preparation
Option 1. @require
Add @violentmonkey/shortcut
to the meta block of our script:
And access all exports under VM.shortcut
:
Option 2. Installation
Another way to use this library is to install it with NPM and import it. TypeScript IntelliSense should work out of box in this way, but we will need a bundler to compile our script.
It is highly recommended to use our generator to initiate a userscript project with modern syntax support.
If we are done with the bundler, we can install @violentmonkey/shortcut as a dependency:
And use it in our code:
Quick Start
Easily register a keyboard shortcut:
Register a key sequence:
Disable and enable all key bindings at any time:
Shortcut for Menu Command
Register a menu command with a shortcut:
See @violentmonkey/shortcut for more advanced usage.