Menu

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

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

DEV Community·hiyoyo·6 months ago
#4NFQR7Zp
#software#rust#tauri#shortcut#window
Reading 0:00
15s threshold

All tests run on an 8-year-old MacBook Air.

A global shortcut fires even when your app isn't focused. For a menubar app, this is essential — Cmd+Shift+H should open the panel regardless of what the user is doing.

Tauri v2 has a global shortcut plugin. It works, but there are a few things worth knowing before you reach for it.

Basic setup

# Cargo.toml [dependencies] tauri-plugin-global-shortcut = "2"

Enter fullscreen mode

Exit fullscreen mode

use tauri_plugin_global_shortcut ::{ GlobalShortcutExt , Shortcut };

fn main () { tauri :: Builder :: default () .plugin ( tauri_plugin_global_shortcut :: Builder :: new () .build ()) .setup (| app | { let shortcut : Shortcut = "CmdOrCtrl+Shift+H" .parse () ? ;

app .global_shortcut () .on_shortcut ( shortcut , | app , _shortcut , _event | { if let Some ( window ) = app .get_webview_window ( "main" ) { if window .is_visible () .unwrap_or ( false ) { window .hide () .unwrap (); } else { window .show () .unwrap (); window .set_focus () .unwrap (); } } }) ?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.