Skip to main content

globalShortcut

@tauri-apps/api / globalShortcut

Module: 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): void

Parameters​
NameType
shortcutstring
Returns​

void

Defined in​

globalShortcut.ts:29

Functions​

isRegistered​

isRegistered(shortcut): 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');

Parameters​

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

Returns​

Promise<boolean>

A promise resolving to the state.


register​

register(shortcut, handler): Promise<void>

Register a global shortcut.

Example

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

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, handler): 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`);
});

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): Promise<void>

Unregister a global shortcut.

Example

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

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();

Returns​

Promise<void>