Pular para o conteúdo

Global Shortcuts

Este conteúdo não está disponível em sua língua ainda.

Global shortcuts are system-wide keyboard shortcuts that fire regardless of which application currently has focus, for as long as your Wails application is running. They are ideal for show/hide hotkeys, quick capture tools, media controls, and other features that users expect to reach from anywhere.

Global shortcuts are built directly on each platform’s native facilities and add no third party dependencies.

The manager is available on the GlobalShortcut property of your application instance:

app := application.New(application.Options{
Name: "Global Shortcuts Demo",
})
globalShortcuts := app.GlobalShortcut

Register takes an accelerator and a callback. The callback runs on its own goroutine whenever the shortcut is pressed.

err := app.GlobalShortcut.Register("CmdOrCtrl+Shift+G", func() {
// Runs even when another application is focused.
window.Show()
window.Focus()
})
if err != nil {
app.Logger.Error("could not register shortcut", "error", err)
}

You can register shortcuts before calling app.Run. The binding with the operating system is then performed automatically when the application starts.

Global shortcuts use the same accelerator format as menu accelerators and key bindings:

"CmdOrCtrl+Shift+G" // Command on macOS, Control elsewhere
"Ctrl+Alt+K" // Control + Alt + K
"Cmd+Option+Space" // Command + Option + Space (macOS)
"Super+D" // Super / Windows / Logo key + D
"Ctrl+Shift+F5" // Function keys are supported

CmdOrCtrl resolves to Command on macOS and Control on Windows and Linux, which makes it convenient for cross-platform shortcuts.

// Check whether a shortcut is registered (modifier order does not matter).
registered := app.GlobalShortcut.IsRegistered("Ctrl+Shift+G")
// List every shortcut this application has registered.
for _, accelerator := range app.GlobalShortcut.GetAll() {
app.Logger.Info("global shortcut", "accelerator", accelerator)
}
// Release a single shortcut.
app.GlobalShortcut.Unregister("Ctrl+Shift+G")
// Release everything (also done automatically on shutdown).
app.GlobalShortcut.UnregisterAll()

All registered shortcuts are released automatically when the application exits, so you do not need to clean them up manually.

What Happens When the Same Shortcut Is Registered Twice

Section titled “What Happens When the Same Shortcut Is Registered Twice”

There are two distinct cases, and Wails handles them differently.

The same application registers a shortcut twice

Section titled “The same application registers a shortcut twice”

This is resolved by Wails itself and behaves identically on every platform. The second Register call returns an error and the original binding is left in place (“error and preserve”). This keeps behavior predictable and surfaces the mistake instead of silently replacing a working shortcut.

app.GlobalShortcut.Register("Ctrl+Shift+G", showWindow) // ok
err := app.GlobalShortcut.Register("Shift+Ctrl+G", doSomething) // err: already registered
// showWindow is still the active callback for this shortcut.

If you want to change the callback for a shortcut, Unregister it first and then Register it again.

Another application already owns the shortcut

Section titled “Another application already owns the shortcut”

This is decided by the operating system, so the result is platform specific:

PlatformBehavior when another application owns the shortcut
macOSRegistration succeeds. macOS allows several applications to register the same hot key, so your callback is added alongside the existing owner rather than rejected.
WindowsRegistration fails and Register returns an error. The application that registered the shortcut first keeps it.
Linux (X11)Registration fails and Register returns an error, because the X server refuses a second grab of the same combination.
Linux (Wayland)The compositor arbitrates. The user is typically asked to approve or choose the binding through the desktop’s global shortcuts dialog.

Because of these differences, always check the error returned by Register and provide a fallback shortcut or user feedback when a shortcut cannot be claimed.

Global shortcuts use the Carbon Event Manager’s hot key API. This is the standard mechanism for system-wide hot keys on macOS and does not require Accessibility permission.

Hot keys are bound to physical key positions, so a shortcut maps to the key in the standard ANSI/QWERTY position on non-QWERTY layouts.

package main
import (
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
func main() {
app := application.New(application.Options{
Name: "Global Shortcuts Demo",
})
window := app.Window.New()
// Bring the window to the front from anywhere.
if err := app.GlobalShortcut.Register("CmdOrCtrl+Shift+G", func() {
window.Show()
window.Focus()
}); err != nil {
log.Printf("could not register show shortcut: %v", err)
}
// Hide the window from anywhere.
if err := app.GlobalShortcut.Register("CmdOrCtrl+Shift+H", func() {
window.Hide()
}); err != nil {
log.Printf("could not register hide shortcut: %v", err)
}
if err := app.Run(); err != nil {
log.Fatal(err)
}
}