Keyboard Shortcuts

Table of Contents

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.


Option 1. @require

Add @violentmonkey/shortcut to the meta block of our script:

// ==UserScript==
// ...
// @require
// ==/UserScript==

And access all exports under VM.shortcut:

VM.shortcut.register('c-i', () => {/* c-i pressed */});

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:

$ npm i @violentmonkey/shortcut

And use it in our code:

import { register } from '@violentmonkey/shortcut';

register('c-i', () => {/* c-i pressed */});


Now we can easily register a keyboard shortcut:

VM.shortcut.register('c-i', () => {
  console.log('You just pressed Ctrl-I');

We can even register a key sequence:

VM.shortcut.register('c-a c-b', () => {
  console.log('You just pressed Ctrl-A Ctrl-B sequence');

We can disable and enable all key bindings at any time:

// Disable all key bindings temporarily

// Re-enable key bindings

Open Chat