input
分层
只有 api/stable/ui_input.txt 列出的符号受兼容性承诺约束。标有「实验性」的符号,以及类型下「实验性成员」列出的字段,任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。
import "github.com/tangoui/tango/ui/input"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package input defines the events a platform host feeds into the Tango self-drawn UI (pointer, multi-touch, wheel, keyboard, committed text and IME composition), the cursor shapes the UI asks the host to show, and the focus/Tab-order bookkeeping shared by focusable widgets.
All coordinates are logical pixels relative to the window's content area.
API tiers: only the symbols listed in api/stable/ui_input.txt are covered by the compatibility promise of docs/release.md ("API 稳定性"); every other exported symbol is Experimental and may change in any 0.x release. The API reference on the documentation site marks the experimental symbols.
常量
const TextStateWindow = 256TextStateWindow is the number of runes TextInputState.Text carries at most on each side of the selection.
实验性
类型
Button
type Button uint8Button is a mouse button.
Append-only: never reorder or insert; the zero value stays ButtonLeft.
const (
ButtonLeft Button = iota
ButtonRight
ButtonMiddle
)Capitalize
type Capitalize uint8Capitalize is the automatic capitalization a soft keyboard applies.
Append-only: never reorder or insert; the zero value stays CapitalizeNone.
const (
CapitalizeNone Capitalize = iota
CapitalizeWords
CapitalizeSentences
)Composition
type Composition struct {
Text string
Cursor int
}Composition reports the IME pre-edit (composition) string. An empty Text ends the composition without committing; the commit itself arrives as TextInput. Cursor is the caret position in runes within Text.
Cursor
type Cursor uint8Cursor is a pointer shape requested by the UI.
Append-only: never reorder or insert; the zero value stays CursorDefault.
const (
CursorDefault Cursor = iota
CursorPointer
CursorText
CursorNotAllowed
CursorResizeEW
CursorResizeNS
CursorGrab
CursorGrabbing
CursorMove
// CursorCopy is the arrow with a plus badge shown while a drag will
// copy rather than move (macOS dragCopyCursor, X11/Wayland "copy").
// Platforms without such a shape show the default arrow.
CursorCopy
)EnterKey
type EnterKey uint8EnterKey is the label of the soft keyboard's return key. Pressing it still arrives as a KeyEnter key event.
Append-only: never reorder or insert; the zero value stays EnterDefault.
const (
EnterDefault EnterKey = iota // platform default for the field
EnterDone
EnterGo
EnterNext
EnterSearch
EnterSend
EnterNewline // inserts a line break (multi-line fields)
)func (EnterKey) String
func (k EnterKey) String() stringEvent
type Event interface {
// contains filtered or unexported methods
}Event is any input event.
FocusOrder 实验性
type FocusOrder[ID comparable] struct {
// contains filtered or unexported fields
}FocusOrder records focusable IDs in paint order for one frame and answers Tab / Shift+Tab navigation.
func (*FocusOrder[ID]) Add 实验性
func (f *FocusOrder[ID]) Add(id ID)Add appends a focusable ID.
func (*FocusOrder[ID]) AddNoTab 实验性
func (f *FocusOrder[ID]) AddNoTab(id ID)AddNoTab appends an ID that can hold focus (after a click) but that Tab navigation skips, such as selectable text.
func (*FocusOrder[ID]) Contains 实验性
func (f *FocusOrder[ID]) Contains(id ID) boolContains reports whether id was registered.
func (*FocusOrder[ID]) Len 实验性
func (f *FocusOrder[ID]) Len() intLen returns the number of registered IDs.
func (*FocusOrder[ID]) Next 实验性
func (f *FocusOrder[ID]) Next(cur ID, back bool) (ID, bool)Next returns the ID after cur (before it when back), wrapping around. If cur is not registered the first (or last) ID is returned.
func (*FocusOrder[ID]) Reset 实验性
func (f *FocusOrder[ID]) Reset()Reset clears the order for a new frame.
Key
type Key uint16Key identifies a physical/logical key.
Append-only: never reorder or insert; the zero value stays KeyUnknown.
const (
KeyUnknown Key = iota
KeyEnter
KeyTab
KeyEscape
KeyBackspace
KeyDelete
KeyLeft
KeyRight
KeyUp
KeyDown
KeyHome
KeyEnd
KeyPageUp
KeyPageDown
KeySpace
KeyA
KeyC
KeyV
KeyX
KeyZ
KeyY
// Remaining letters and digits (appended so earlier values stay stable).
KeyB
KeyD
KeyE
KeyF
KeyG
KeyH
KeyI
KeyJ
KeyK
KeyL
KeyM
KeyN
KeyO
KeyP
KeyQ
KeyR
KeyS
KeyT
KeyU
KeyW
Key0
Key1
Key2
Key3
Key4
Key5
Key6
Key7
Key8
Key9
KeyF1
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyF10
KeyF11
KeyF12
// Modifier keys themselves (pressed / released on their own). Mods on
// the event already includes (on press) or excludes (on release) the
// modifier.
KeyShiftLeft
KeyShiftRight
KeyCtrlLeft
KeyCtrlRight
KeyAltLeft // Option on macOS
KeyAltRight // Option on macOS
KeyMetaLeft // Command on macOS, Windows key elsewhere
KeyMetaRight
// Punctuation, named after the US layout key.
KeyMinus // -
KeyEqual // =
KeyBracketLeft // [
KeyBracketRight // ]
KeyBackslash // \
KeySemicolon // ;
KeyQuote // '
KeyComma // ,
KeyPeriod // .
KeySlash // /
KeyBackquote // `
// Numeric keypad.
KeyNumpad0
KeyNumpad1
KeyNumpad2
KeyNumpad3
KeyNumpad4
KeyNumpad5
KeyNumpad6
KeyNumpad7
KeyNumpad8
KeyNumpad9
KeyNumpadAdd
KeyNumpadSubtract
KeyNumpadMultiply
KeyNumpadDivide
KeyNumpadDecimal
KeyNumpadEnter
KeyInsert
KeyCapsLock
KeyContextMenu
// KeyBack is never a Key of its own: it is the Code of the Escape event
// that stands for Android's system Back (see KeyEvent).
KeyBack
// KeyPaste is the Code of a synthetic V event from a native Paste menu.
KeyPaste
)实验性 KeyBack、KeyPaste
func KeyForRune
func KeyForRune(r rune) KeyKeyForRune maps an ASCII letter (either case) or digit to its Key, or KeyUnknown. Hosts use it for layout-aware shortcut keys.
func KeyNamed
func KeyNamed(name string) (Key, bool)KeyNamed is the inverse of String; it reports false for unknown names.
func (Key) Base
func (k Key) Base() KeyBase folds keypad keys that duplicate a main key onto it (NumpadEnter → Enter, Numpad0..9 → 0..9) and returns other keys unchanged, so shortcuts can match either.
func (Key) Modifier
func (k Key) Modifier() ModifiersModifier returns the modifier bit a modifier key controls (0 for other keys). Hosts that only learn the new modifier state (macOS flagsChanged:) use it to decide whether the key went down or up.
func (Key) Rune
func (k Key) Rune() runeRune returns the character a letter, digit, punctuation, space or keypad key produces on the US layout without Shift (letters lower-case), or 0 for other keys.
func (Key) String
func (k Key) String() stringString returns the key name ("A", "7", "F5", "ShiftLeft", "Minus", "NumpadEnter"...). Unknown values print as "Key(n)".
KeyEvent
type KeyEvent struct {
Key Key
Code Key // physical key as reported by the platform; 0 means same as Key
Down bool
Repeat bool
Mods Modifiers
// HID is the physical key as a USB HID usage on the Keyboard/Keypad
// page (0x07), without the page: 0x04 is A, 0x28 Enter, 0xE5 right
// Shift. It covers every key of a full ANSI, ISO or JIS keyboard, also
// those Key has no name for. 0 means unknown.
HID uint16
// Native is the platform's own code for the key: the NSEvent keyCode
// on macOS; the scan code on Windows, extended keys with an 0xE000
// prefix (right Ctrl is 0xE01D, Pause 0xE11D); the XKB keycode on
// Linux. 0 means unknown.
Native uint32
}KeyEvent is a key press or release. KeyEvent is a key press or release.
Platform hosts report Key as follows: letters and digits by the character they produce in the current keyboard layout (KeyForRune); every other key (punctuation, F-keys, keypad, left/right modifiers) by its physical position on a US layout. Keypad keys follow NumLock: with NumLock on they are reported as Numpad* keys (Code 0); with NumLock off as the navigation key they stand for (Up, Down, Left, Right, Home, End, PageUp, PageDown, Insert, Delete) with the physical Numpad* key in Code, and keypad 5 as KeyUnknown with Code KeyNumpad5. macOS has no NumLock and always reports Numpad* keys. A key's release reports the same Key/Code as its press even if NumLock changed in between. NumpadEnter is distinct from Enter.
Before components see an event, the runner sets Code to the reported key and folds Key with Key.Base(), so NumpadEnter arrives as KeyEnter and Numpad digits as digits; use Code to tell them apart.
Android's system Back (button or gesture), while the UI takes it (ui.BackWindow), is reported as Key KeyEscape with Code KeyBack: it closes overlays like Escape, and widgets that claim Escape for themselves (ui.Context.ClaimEscape) can tell it from a keyboard Esc.
For a modifier key's own event, Mods includes that modifier while it is down and excludes it on release, unless the same modifier on the other side is still held.
Modifiers
type Modifiers uint8Modifiers is a set of held modifier keys.
Append-only: a new flag takes the next free bit; existing bits never change.
const (
ModShift Modifiers = 1 << iota
ModCtrl
ModAlt
ModMeta // Command on macOS, Windows key elsewhere
)func (Modifiers) Has
func (m Modifiers) Has(m2 Modifiers) boolHas reports whether all of m2 are held.
func (Modifiers) Shortcut
func (m Modifiers) Shortcut() boolShortcut reports whether the platform shortcut modifier (Cmd on Apple platforms, Ctrl elsewhere) is held.
Pointer
type Pointer struct {
Kind PointerKind
Pos geom.Point
Button Button
Mods Modifiers
// Clicks is the click count for Press (1 single, 2 double...). Hosts
// that do not know may leave it 0; the UI then counts itself.
Clicks int
// Source is what produced the event: a mouse or trackpad (the zero
// value) or a finger (pointer events the UI synthesizes from the
// primary touch, or hosts that report touches as pointers).
Source PointerSource
// Time is when the platform produced the event, on a monotonic clock
// with an arbitrary origin: compare it only with Time of other events.
// 0 means unknown.
Time time.Duration
}Pointer is a mouse/touch/pen event.
PointerKind
type PointerKind uint8PointerKind is the pointer action.
Append-only: never reorder or insert; the zero value stays Move.
const (
Move PointerKind = iota
Press
Release
Leave // pointer left the window
Cancel // gesture cancelled by the system
)PointerSource
type PointerSource uint8PointerSource tells mouse-made pointer events from touch-made ones.
Append-only: never reorder or insert; the zero value stays SourceMouse.
const (
SourceMouse PointerSource = iota // mouse, trackpad, pen hovering
SourceTouch // a finger
)Scroll
type Scroll struct {
Pos geom.Point
DX, DY float32
Mods Modifiers
}Scroll is a wheel / trackpad scroll. Positive DY scrolls content up (reveals what is below), in logical pixels.
SelectionChange
type SelectionChange struct{ Start, End int }SelectionChange asks the focused field to select the runes [Start, End) (field positions; Start == End: move the caret).
TextEdit
type TextEdit struct {
Start, End int
Text string
}TextEdit asks the focused field to replace the runes [Start, End) (field positions) with Text: an input method's autocorrection, a deleteSurroundingText, the commit after setComposingRegion. The caret ends after the inserted text unless a SelectionChange follows.
TextHints
type TextHints struct {
Kind TextKind
Enter EnterKey
Multiline bool
// NoAutocorrect turns autocorrection and suggestions off; they are on
// by default. TextPassword fields never get them (see Autocorrect).
NoAutocorrect bool
Capitalize Capitalize
}TextHints describe a text field to the input method.
func (TextHints) Autocorrect
func (h TextHints) Autocorrect() boolAutocorrect reports whether the input method may correct and suggest words: not for passwords, nor when NoAutocorrect is set.
TextInput
type TextInput struct{ Text string }TextInput carries committed text (typing, paste from IME, dictation).
TextInputState
type TextInputState struct {
Hints TextHints
// Text is the field's text around the selection: at most
// TextStateWindow runes before SelStart and after SelEnd.
Text string
// Offset is the field position of Text's first rune.
Offset int
// SelStart..SelEnd is the selection (SelStart == SelEnd: a caret).
SelStart, SelEnd int
// CompStart..CompEnd is the composing (pre-edit) region; both are -1
// when there is none.
CompStart, CompEnd int
}TextInputState describes the focused text field near the caret. Offsets are rune positions relative to the start of Text; Offset places Text within the whole field, so field position = Offset + a Text offset.
func (TextInputState) HasComposition
func (s TextInputState) HasComposition() boolHasComposition reports whether the state has a composing region.
TextKind
type TextKind uint8TextKind selects the soft keyboard layout of a text field.
Append-only: never reorder or insert; the zero value stays TextPlain.
const (
TextPlain TextKind = iota // general text
TextNumber // digits only
TextDecimal // digits and a decimal separator
TextPhone // telephone keypad
TextEmail // e-mail address
TextURL // web address
TextPassword // secure entry: no echo, no suggestions
TextSearch // search query
)func (TextKind) String
func (k TextKind) String() stringTouchEvent
type TouchEvent struct {
ID int64
Phase TouchPhase
Pos geom.Point
Pressure float32
Time time.Time
}TouchEvent is one change of one finger on a touch screen. A frame may carry several (one per finger that changed); hosts send them in the order the platform reported them.
ID identifies the finger from TouchBegan to TouchEnded / TouchCancelled and must not be reused while the finger is down (UITouch pointer, Android pointer ID, PointerEvent.pointerId). Pos is in logical pixels relative to the content area, like Pointer.Pos. Pressure is normalized to 0..1 (1 = a typical firm press); 0 means the device does not report it. Time is the platform timestamp of the change; zero means unknown and the runner uses the frame time.
The runner synthesizes Pointer events (Move + Press, Move, Release) from the first finger of a gesture so widgets without touch handling keep working; see Runner.SetTouchPointerEmulation. Hosts must therefore not send their own emulated mouse events for touches they report here.
func (TouchEvent) Done
func (e TouchEvent) Done() boolDone reports whether the finger left the screen with this event.
TouchPhase
type TouchPhase uint8TouchPhase is the stage of one finger's contact.
Append-only: never reorder or insert; the zero value stays TouchBegan.
const (
TouchBegan TouchPhase = iota // finger went down
TouchMoved // finger moved (or its pressure changed)
TouchEnded // finger lifted
TouchCancelled // the system took the touch away (incoming call, OS gesture)
)func (TouchPhase) String
func (p TouchPhase) String() stringWindowFocus
type WindowFocus struct{ Focused bool }WindowFocus reports the window gaining or losing key focus.