Documentation
¶
Overview ¶
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.
Index ¶
- Constants
- type Button
- type Capitalize
- type Composition
- type Cursor
- type EnterKey
- type Event
- type FocusOrder
- type Key
- type KeyEvent
- type Modifiers
- type Pointer
- type PointerKind
- type PointerSource
- type Scroll
- type SelectionChange
- type TextEdit
- type TextHints
- type TextInput
- type TextInputState
- type TextKind
- type TouchEvent
- type TouchPhase
- type WindowFocus
Constants ¶
const TextStateWindow = 256
TextStateWindow is the number of runes TextInputState.Text carries at most on each side of the selection.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Button ¶
type Button uint8
Button is a mouse button.
Append-only: never reorder or insert; the zero value stays ButtonLeft.
type Capitalize ¶
type Capitalize uint8
Capitalize 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 )
type Composition ¶
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.
type Cursor ¶
type Cursor uint8
Cursor 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 )
type EnterKey ¶
type EnterKey uint8
EnterKey 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.
type Event ¶
type Event interface {
// contains filtered or unexported methods
}
Event is any input event.
type 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]) 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) bool
Contains reports whether id was registered.
func (*FocusOrder[ID]) Len ¶
func (f *FocusOrder[ID]) Len() int
Len 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.
type Key ¶
type Key uint16
Key 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 )
func KeyForRune ¶
KeyForRune maps an ASCII letter (either case) or digit to its Key, or KeyUnknown. Hosts use it for layout-aware shortcut keys.
func (Key) Base ¶
Base 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 ¶
Modifier 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.
type 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.
type Modifiers ¶
type Modifiers uint8
Modifiers is a set of held modifier keys.
Append-only: a new flag takes the next free bit; existing bits never change.
type 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.
type PointerKind ¶
type PointerKind uint8
PointerKind 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 )
type PointerSource ¶
type PointerSource uint8
PointerSource 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 )
type Scroll ¶
Scroll is a wheel / trackpad scroll. Positive DY scrolls content up (reveals what is below), in logical pixels.
type 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).
type TextEdit ¶
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.
type 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 ¶
Autocorrect reports whether the input method may correct and suggest words: not for passwords, nor when NoAutocorrect is set.
type TextInput ¶
type TextInput struct{ Text string }
TextInput carries committed text (typing, paste from IME, dictation).
type 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() bool
HasComposition reports whether the state has a composing region.
type TextKind ¶
type TextKind uint8
TextKind selects the soft keyboard layout of a text field.
Append-only: never reorder or insert; the zero value stays TextPlain.
type TouchEvent ¶
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() bool
Done reports whether the finger left the screen with this event.
type TouchPhase ¶
type TouchPhase uint8
TouchPhase 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() string
type WindowFocus ¶
type WindowFocus struct{ Focused bool }
WindowFocus reports the window gaining or losing key focus.