DEV Community

Cover image for Global Keyboard Shortcuts in Tauri v2 — The Right Way and the Wrong Way
hiyoyo
hiyoyo

Posted on

Global Keyboard Shortcuts in Tauri v2 — The Right Way and the Wrong Way

All tests run on an 8-year-old MacBook Air.
All results from shipping 7 Mac apps as a solo developer. No sponsored opinion.
Global shortcuts in Tauri v2 changed significantly from v1. The plugin API is cleaner now, but there are still ways to get it wrong.
Here's what I learned building a menubar app with a system-wide shortcut.

The basics
Add the plugin:
toml# Cargo.toml
tauri-plugin-global-shortcut = "2"
json// package.json
"@tauri-apps/plugin-global-shortcut": "^2.0.0"
Register in main.rs:
rustuse tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut};

tauri::Builder::default()
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.setup(|app| {
let shortcut = Shortcut::new(
Some(Modifiers::SUPER | Modifiers::SHIFT),
Code::KeyH
);
app.global_shortcut().register(shortcut)?;
Ok(())
})

The wrong way: hardcoded shortcuts
Hardcoding Cmd+Shift+H is fine for your machine. It's a conflict on someone else's machine where that shortcut is already taken by another app.
Build user-configurable shortcuts. Store the preference. Register on launch. Re-register when the user changes it.
rustlet stored = get_shortcut_from_prefs(); // read from SQLite or config
app.global_shortcut().register(stored)?;

The macOS permission reality
On macOS, global shortcuts require Accessibility permissions if the app doesn't have focus. This means:

User installs your app
First time they use the global shortcut, nothing happens
They think it's broken

The fix: check for Accessibility permissions on launch and prompt the user if they're missing. Don't silently fail.
rust// In your setup, after registering the shortcut
// Emit an event to the frontend if permissions may be missing
// Let the UI guide the user to System Settings > Privacy > Accessibility
This is the most common support ticket for menubar apps with global shortcuts. Handle it proactively.

Unregistering on app exit
Tauri v2 handles cleanup automatically when the app exits. But if you're re-registering shortcuts dynamically (user changed the shortcut in settings), unregister the old one first:
rustapp.global_shortcut().unregister(old_shortcut)?;
app.global_shortcut().register(new_shortcut)?;
Double-registered shortcuts cause silent failures on some macOS versions.

The verdict
Global shortcuts in Tauri v2 work well. The plugin API is clean. The two things that will cause you problems are hardcoded shortcuts and missing the Accessibility permissions flow.
Build configurability and permission handling in before your first release.

If this was useful, a ❤️ helps more than you'd think — thanks!
Hiyoko PDF Vault → https://hiyokoko.gumroad.com/l/HiyokoPDFVault
X → @hiyoyok

Top comments (0)