Passer au contenu principal

globalShortcut

Register global shortcuts.

This package is also accessible with window.__TAURI__.globalShortcut when build.withGlobalTauri in tauri.conf.json is set to true.

The APIs must be added to tauri.allowlist.globalShortcut in tauri.conf.json:

{
"tauri": {
"allowlist": {
"globalShortcut": {
"all": true // enable all global shortcut APIs
}
}
}
}

It is recommended to allowlist only the APIs you use for optimal bundle size and security.

Type Aliases​

ShortcutHandler​

ShortcutHandler: (shortcut: string) => void

Type declaration

(shortcut: string): void

Parameters

NameType
shortcutstring

Returns: void

Defined in: globalShortcut.ts:29

Functions​

isRegistered​

isRegistered(shortcut: string): Promise<boolean>

Determines whether the given shortcut is registered by this application or not.

Example

import { isRegistered } from '@tauri-apps/api/globalShortcut';
const isRegistered = await isRegistered('CommandOrControl+P');

Since: 1.0.0

Parameters

NameTypeDescription
shortcutstringArray of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q

Returns: Promise<boolean>

register​

register(shortcut: string, handler: ShortcutHandler): Promise<void>

Register a global shortcut.

Example

import { register } from '@tauri-apps/api/globalShortcut';
await register('CommandOrControl+Shift+C', () => {
console.log('Shortcut triggered');
});

Since: 1.0.0

Parameters

NameTypeDescription
shortcutstringShortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q
handlerShortcutHandlerShortcut handler callback - takes the triggered shortcut as argument

Returns: Promise<void>

registerAll​

registerAll(shortcuts: string[], handler: ShortcutHandler): Promise<void>

Register a collection of global shortcuts.

Example

import { registerAll } from '@tauri-apps/api/globalShortcut';
await registerAll(['CommandOrControl+Shift+C', 'Ctrl+Alt+F12'], (shortcut) => {
console.log(`Shortcut ${shortcut} triggered`);
});

Since: 1.0.0

Parameters

NameTypeDescription
shortcutsstring[]Array of shortcut definitions, modifiers and key separated by "+" e.g. CmdOrControl+Q
handlerShortcutHandlerShortcut handler callback - takes the triggered shortcut as argument

Returns: Promise<void>

unregister​

unregister(shortcut: string): Promise<void>

Unregister a global shortcut.

Example

import { unregister } from '@tauri-apps/api/globalShortcut';
await unregister('CmdOrControl+Space');

Since: 1.0.0

Parameters

NameTypeDescription
shortcutstringshortcut definition, modifiers and key separated by "+" e.g. CmdOrControl+Q

Returns: Promise<void>

unregisterAll​

unregisterAll(): Promise<void>

Unregisters all shortcuts registered by the application.

Example

import { unregisterAll } from '@tauri-apps/api/globalShortcut';
await unregisterAll();

Since: 1.0.0

Returns: Promise<void>