Global Shortcut
Register global shortcuts.
Supported Platforms
- Windows
- Linux
- macOS
Setup
This plugin requires a Rust version of at least 1.75
Install the global-shortcut plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add global-shortcutyarn run tauri add global-shortcutpnpm tauri add global-shortcutbun tauri add global-shortcutcargo tauri add global-shortcut-
Install the global-shortcut plugin by adding the following to your
Cargo.tomlfile:src-tauri/Cargo.toml # you can add the dependencies on the `[dependencies]` section if you do not target mobile[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]tauri-plugin-global-shortcut = "2.0.0-rc"# alternatively with Git:tauri-plugin-global-shortcut = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } -
Modify
lib.rsto initialize the plugin:src-tauri/src/lib.rs fn run() {tauri::Builder::default().plugin(tauri_plugin_global_shortcut::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-global-shortcutyarn add @tauri-apps/plugin-global-shortcutpnpm add @tauri-apps/plugin-global-shortcutbun add @tauri-apps/plugin-global-shortcut
Usage
The global-shortcut plugin is available in both JavaScript and Rust.
import { register } from '@tauri-apps/plugin-global-shortcut';// when using `"withGlobalTauri": true`, you may use// const { register } = window.__TAURI_PLUGIN_GLOBAL_SHORTCUT__;
await register('CommandOrControl+Shift+C', () => { console.log('Shortcut triggered');});fn run() { tauri::Builder::default() .setup(|app| { #[cfg(desktop)] { use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut};
let ctrl_n_shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::KeyN); app.handle().plugin( tauri_plugin_global_shortcut::Builder::with_handler(move |_app, shortcut| { println!("{:?}", shortcut); if shortcut == &ctrl_n_shortcut { println!("Ctrl-N Detected!"); } }) .build(), )?;
app.global_shortcut().register(ctrl_n_shortcut)?; } Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}Permissions
By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
See the Capabilities Overview for more information.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "global-shortcut:allow-is-registered", "global-shortcut:allow-register", "global-shortcut:allow-unregister" ]}| Permission | Description |
|---|---|
global-shortcut:allow-is-registered | Enables the is_registered command without any pre-configured scope. |
global-shortcut:deny-is-registered | Denies the is_registered command without any pre-configured scope. |
global-shortcut:allow-register | Enables the register command without any pre-configured scope. |
global-shortcut:deny-register | Denies the register command without any pre-configured scope. |
global-shortcut:allow-register-all | Enables the register_all command without any pre-configured scope. |
global-shortcut:deny-register-all | Denies the register_all command without any pre-configured scope. |
global-shortcut:allow-unregister | Enables the unregister command without any pre-configured scope. |
global-shortcut:deny-unregister | Denies the unregister command without any pre-configured scope. |
global-shortcut:allow-unregister-all | Enables the unregister_all command without any pre-configured scope. |
global-shortcut:deny-unregister-all | Denies the unregister_all command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT