hotkey

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hotkey registers keyboard shortcuts.

Register makes a system-wide (global) shortcut that fires even when the app is in the background:

hk, err := hotkey.Register(a, "CmdOrCtrl+Shift+K", func() { win.Show() })
...
hk.Unregister()

Bind makes a shortcut that fires only while a surface's page has the keyboard focus; it needs no platform support (it listens to keydown in the page and swallows the key):

b, err := hotkey.Bind(win, "CmdOrCtrl+K", openPalette)

Accelerators use the menu syntax: modifiers CmdOrCtrl, Cmd/Command/Super/ Meta, Ctrl, Alt/Option, Shift joined with "+" to one key (A–Z, 0–9, F1–F24, Enter, Tab, Space, Backspace, Delete, Insert, Esc, Up/Down/Left/ Right, Home, End, PageUp, PageDown, punctuation, num0–num9). CmdOrCtrl is Command on macOS and Control elsewhere.

Callbacks run on the main thread. Global shortcuts: macOS (Carbon hot keys, no accessibility permission needed), Windows (RegisterHotKey), Linux on X11 (XGrabKey); Wayland has no global shortcut API for apps and returns ErrUnsupported.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadAccelerator = errors.New("hotkey: bad accelerator")

ErrBadAccelerator is wrapped by the errors of Validate, Register and Bind for an accelerator that does not parse.

View Source
var ErrUnregistered = errors.New("hotkey: not registered")

ErrUnregistered is returned by Unregister on a hotkey that is no longer registered.

View Source
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

Functions

func Unregister

func Unregister(h *Hotkey) error

Unregister is h.Unregister().

func Validate

func Validate(accelerator string) error

Validate reports whether accelerator is well formed for this platform, without registering anything. Its error wraps ErrBadAccelerator and says what is wrong.

Types

type Binding

type Binding struct {
	// contains filtered or unexported fields
}

Binding is a shortcut bound to one surface's page.

func Bind

func Bind(s *tango.Surface, accelerator string, fn func()) (*Binding, error)

Bind makes accelerator call fn (on the main thread) while s's page has the keyboard focus. The key event does not reach the page. Bindings survive navigation and end when the surface closes or with Unbind. If several bindings of s share an accelerator, the first bound fires.

func (*Binding) Accelerator

func (b *Binding) Accelerator() string

Accelerator returns the accelerator b was bound with.

func (*Binding) Unbind

func (b *Binding) Unbind()

Unbind removes the binding; the key reaches the page again. Unbinding twice does nothing.

type Hotkey

type Hotkey struct {
	// contains filtered or unexported fields
}

Hotkey is a registered global shortcut.

func Register

func Register(a *tango.App, accelerator string, fn func()) (*Hotkey, error)

Register registers a system-wide shortcut; fn runs on the main thread each time it is pressed. It fails if the accelerator is malformed, if the platform cannot register it (taken by another app or by the system) or with ErrUnsupported.

func (*Hotkey) Accelerator

func (h *Hotkey) Accelerator() string

Accelerator returns the accelerator h was registered with.

func (*Hotkey) Unregister

func (h *Hotkey) Unregister() error

Unregister removes the shortcut. Unregistering twice returns ErrUnregistered.

type Spec

type Spec struct {
	Ctrl, Alt, Shift, Meta bool
	// Code names the key like KeyboardEvent.code: "KeyK", "Digit1", "F5",
	// "ArrowUp", "Equal", "Numpad5".
	Code string
	// Key is the lower-cased KeyboardEvent.key that also matches ("k",
	// "+", "escape"), "" if only Code does.
	Key string
}

Spec is an accelerator parsed for the running platform (Parse): CmdOrCtrl is resolved to Meta on macOS and Ctrl elsewhere.

func Parse

func Parse(accelerator string) (Spec, error)

Parse parses accelerator for this platform without registering anything. Other shortcut tables (uihost.Shortcut for self-drawn windows) use it so every Tango shortcut API accepts the same syntax. Its error wraps ErrBadAccelerator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL