Skip to content

Commit 1e99624

Browse files
committed
doc: add shortcut usage with GM_registerMenuCommand
1 parent ffea5fd commit 1e99624

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

content/api/gm.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ Returns the command's `caption` since VM2.12.5 or `id` since VM2.15.9.
373373
* <Field name="autoClose?" type="boolean" defaultValue="true" />
374374
Whether to auto-close the popup after the user invoked the command.
375375

376-
If you want to add a shortcut, please see [vm.shortcut](https://github.com/violentmonkey/vm-shortcut).
376+
<InfoBox>
377+
If you want to add a shortcut, please refer to [keyboard shortcut](/guide/keyboard-shortcuts/#shortcut-for-menu-command).
378+
</InfoBox>
377379

378380
Here's how you can change the command in-place, thus preserving its relative position in the list of multiple commands:
379381
```js

content/guide/keyboard-shortcuts/index.mdx

+24-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Add `@violentmonkey/shortcut` to the [meta block](/api/metadata-block/) of our s
3232
And access all exports under `VM.shortcut`:
3333

3434
```js
35-
VM.shortcut.register('c-i', () => {/* c-i pressed */});
35+
const { register } = VM.shortcut;
3636
```
3737

3838
### Option 2. Installation
@@ -51,29 +51,27 @@ And use it in our code:
5151

5252
```js
5353
import { register } from '@violentmonkey/shortcut';
54-
55-
register('c-i', () => {/* c-i pressed */});
5654
```
5755

58-
## Usage
56+
## Quick Start
5957

60-
Now we can easily register a keyboard shortcut:
58+
Easily register a keyboard shortcut:
6159

6260
```js
6361
VM.shortcut.register('c-i', () => {
6462
console.log('You just pressed Ctrl-I');
6563
});
6664
```
6765

68-
We can even register a key sequence:
66+
Register a key sequence:
6967

7068
```js
7169
VM.shortcut.register('c-a c-b', () => {
7270
console.log('You just pressed Ctrl-A Ctrl-B sequence');
7371
});
7472
```
7573

76-
We can disable and enable all key bindings at any time:
74+
Disable and enable all key bindings at any time:
7775

7876
```js
7977
// Disable all key bindings temporarily
@@ -83,4 +81,23 @@ VM.shortcut.disable();
8381
VM.shortcut.enable();
8482
```
8583

84+
## Shortcut for Menu Command
85+
86+
Register a menu command with a shortcut:
87+
88+
```js
89+
const shortcut = 'c-g c-g';
90+
const name = 'Good Game';
91+
const handler = () => alert('Good Game!');
92+
93+
// Register menu command
94+
const menuName = `${name} (${VM.shortcut.reprShortcut(shortcut)})`;
95+
GM_registerMenuCommand(menuName, handler);
96+
97+
// Register shortcut
98+
VM.shortcut.register(shortcut, handler);
99+
```
100+
101+
See [@violentmonkey/shortcut][vm-shortcut] for more advanced usage.
102+
86103
[vm-shortcut]: https://github.com/violentmonkey/vm-shortcut

0 commit comments

Comments
 (0)