Skip to content

hotkey ​

go
import "github.com/tangoui/tango/sys/hotkey"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

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.

变量 ​

go
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.

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

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

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

函数 ​

func Unregister ​

go
func Unregister(h *Hotkey) error

Unregister is h.Unregister().

func Validate ​

go
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.

类型 ​

Binding ​

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

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

func Bind ​

go
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 ​

go
func (b *Binding) Accelerator() string

Accelerator returns the accelerator b was bound with.

func (*Binding) Unbind ​

go
func (b *Binding) Unbind()

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

Hotkey ​

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

Hotkey is a registered global shortcut.

func Register ​

go
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 ​

go
func (h *Hotkey) Accelerator() string

Accelerator returns the accelerator h was registered with.

func (*Hotkey) Unregister ​

go
func (h *Hotkey) Unregister() error

Unregister removes the shortcut. Unregistering twice returns ErrUnregistered.

Spec ​

go
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 ​

go
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.

以 MIT 许可发布