hotkey
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.
变量
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.
var ErrUnregistered = errors.New("hotkey: not registered")ErrUnregistered is returned by Unregister on a hotkey that is no longer registered.
var ErrUnsupported = platform.ErrUnsupportedErrUnsupported is platform.ErrUnsupported.
函数
func Unregister
func Unregister(h *Hotkey) errorUnregister is h.Unregister().
func Validate
func Validate(accelerator string) errorValidate reports whether accelerator is well formed for this platform, without registering anything. Its error wraps ErrBadAccelerator and says what is wrong.
类型
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() stringAccelerator 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.
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() stringAccelerator returns the accelerator h was registered with.
func (*Hotkey) Unregister
func (h *Hotkey) Unregister() errorUnregister removes the shortcut. Unregistering twice returns ErrUnregistered.
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.