@@ -19,15 +19,28 @@ export interface ShortcutEvent {
19
19
export type ShortcutHandler = ( event : ShortcutEvent ) => void ;
20
20
21
21
/**
22
- * Register a global shortcut.
22
+ * Register a global shortcut or a list of shortcuts.
23
+ *
24
+ * The handler is called when any of the registered shortcuts are pressed by the user.
25
+ *
26
+ * If the shortcut is already taken by another application, the handler will not be triggered.
27
+ * Make sure the shortcut is as unique as possible while still taking user experience into consideration.
28
+ *
23
29
* @example
24
30
* ```typescript
25
31
* import { register } from '@tauri-apps/plugin-global-shortcut';
32
+ *
33
+ * // register a single hotkey
26
34
* await register('CommandOrControl+Shift+C', (event) => {
27
35
* if (event.state === "Pressed") {
28
36
* console.log('Shortcut triggered');
29
37
* }
30
38
* });
39
+ *
40
+ * // or register multiple hotkeys at once
41
+ * await register(['CommandOrControl+Shift+C', 'Alt+A'], (event) => {
42
+ * console.log(`Shortcut ${event.shortcut} triggered`);
43
+ * });
31
44
* ```
32
45
*
33
46
* @param shortcut Shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
@@ -36,97 +49,75 @@ export type ShortcutHandler = (event: ShortcutEvent) => void;
36
49
* @since 2.0.0
37
50
*/
38
51
async function register (
39
- shortcut : string ,
52
+ shortcuts : string | string [ ] ,
40
53
handler : ShortcutHandler ,
41
54
) : Promise < void > {
42
55
const h = new Channel < ShortcutEvent > ( ) ;
43
56
h . onmessage = handler ;
44
57
45
- await invoke ( "plugin:global-shortcut|register" , {
46
- shortcut ,
58
+ return await invoke ( "plugin:global-shortcut|register" , {
59
+ shortcuts : Array . isArray ( shortcuts ) ? shortcuts : [ shortcuts ] ,
47
60
handler : h ,
48
61
} ) ;
49
62
}
50
63
51
64
/**
52
- * Register a collection of global shortcuts.
65
+ * Unregister a global shortcut or a list of shortcuts.
66
+ *
53
67
* @example
54
68
* ```typescript
55
- * import { registerAll } from '@tauri-apps/plugin-global-shortcut';
56
- * await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (event) => {
57
- * console.log(`Shortcut ${event.shortcut} ${event.state}`);
58
- * });
69
+ * import { unregister } from '@tauri-apps/plugin-global-shortcut';
70
+ *
71
+ * // unregister a single hotkey
72
+ * await unregister('CmdOrControl+Space');
73
+ *
74
+ * // or unregister multiple hotkeys at the same time
75
+ * await unregister(['CmdOrControl+Space', 'Alt+A']);
59
76
* ```
60
77
*
61
- * @param shortcuts Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
62
- * @param handler Shortcut handler callback - takes the triggered shortcut as argument
78
+ * @param shortcut shortcut definition (modifiers and key separated by "+" e.g. CmdOrControl+Q), also accepts a list of shortcuts
63
79
*
64
80
* @since 2.0.0
65
81
*/
66
- async function registerAll (
67
- shortcuts : string [ ] ,
68
- handler : ShortcutHandler ,
69
- ) : Promise < void > {
70
- const h = new Channel < ShortcutEvent > ( ) ;
71
- h . onmessage = handler ;
72
-
73
- await invoke ( "plugin:global-shortcut|register_all" , {
74
- shortcuts,
75
- handler : h ,
82
+ async function unregister ( shortcuts : string | string [ ] ) : Promise < void > {
83
+ return await invoke ( "plugin:global-shortcut|unregister" , {
84
+ shortcuts : Array . isArray ( shortcuts ) ? shortcuts : [ shortcuts ] ,
76
85
} ) ;
77
86
}
78
87
79
88
/**
80
- * Determines whether the given shortcut is registered by this application or not.
81
- *
82
- * If the shortcut is registered by another application, it will still return `false`.
89
+ * Unregister all global shortcuts.
83
90
*
84
91
* @example
85
92
* ```typescript
86
- * import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
87
- * const isRegistered = await isRegistered('CommandOrControl+P' );
93
+ * import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
94
+ * await unregisterAll( );
88
95
* ```
89
- *
90
- * @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
91
- *
92
96
* @since 2.0.0
93
97
*/
94
- async function isRegistered ( shortcut : string ) : Promise < boolean > {
95
- return await invoke ( "plugin:global-shortcut|is_registered" , {
96
- shortcut,
97
- } ) ;
98
+ async function unregisterAll ( ) : Promise < void > {
99
+ return await invoke ( "plugin:global-shortcut|unregister_all" , { } ) ;
98
100
}
99
101
100
102
/**
101
- * Unregister a global shortcut.
103
+ * Determines whether the given shortcut is registered by this application or not.
104
+ *
105
+ * If the shortcut is registered by another application, it will still return `false`.
106
+ *
102
107
* @example
103
108
* ```typescript
104
- * import { unregister } from '@tauri-apps/plugin-global-shortcut';
105
- * await unregister('CmdOrControl+Space ');
109
+ * import { isRegistered } from '@tauri-apps/plugin-global-shortcut';
110
+ * const isRegistered = await isRegistered('CommandOrControl+P ');
106
111
* ```
107
112
*
108
113
* @param shortcut shortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
109
114
*
110
115
* @since 2.0.0
111
116
*/
112
- async function unregister ( shortcut : string ) : Promise < void > {
113
- await invoke ( "plugin:global-shortcut|unregister " , {
117
+ async function isRegistered ( shortcut : string ) : Promise < boolean > {
118
+ return await invoke ( "plugin:global-shortcut|is_registered " , {
114
119
shortcut,
115
120
} ) ;
116
121
}
117
122
118
- /**
119
- * Unregisters all shortcuts registered by the application.
120
- * @example
121
- * ```typescript
122
- * import { unregisterAll } from '@tauri-apps/plugin-global-shortcut';
123
- * await unregisterAll();
124
- * ```
125
- *
126
- * @since 2.0.0
127
- */
128
- async function unregisterAll ( ) : Promise < void > {
129
- await invoke ( "plugin:global-shortcut|unregister_all" ) ;
130
- }
131
-
132
- export { register , registerAll , isRegistered , unregister , unregisterAll } ;
123
+ export { register , unregister , unregisterAll , isRegistered } ;
0 commit comments