input

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 3 Imported by: ? modules, 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

Index

Constants

View Source
const DefaultDeadzone = 0.15

DefaultDeadzone is the default value of the Deadzone field in ActionMap. Below the value, input axes are considered inactive, i.e., treated as zero.

View Source
const MaxPads = 4 // Maximum number of gamepads supported. NOTE: Review when adding configurable pad count support.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action string

Action is a human-readable alias for a game-level input role, such as "jump" or "move_x". It is used as the key in the Bindings map to associate actions with physical inputs.

type ActionMap

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

ActionMap resolves bound physical inputs into per-action state. Update resolves one frame, Tick delivers one tick, and the query methods report the results. Deadzone is the pad axis deadzone, defaulting to DefaultDeadzone.

A nil ActionMap reads zero on every query, so the context can publish nil when no bindings were configured. The mutators (Update, Tick, SetBindings) require a real map.

func New

func New(bindings Bindings, deadzone float64) (*ActionMap, error)

New returns an ActionManager over a copy of bindings. A deadzone of zero selects DefaultDeadzone. Nil bindings, or a deadzone outside [0, 1], are errors.

func (*ActionMap) Axis

func (am *ActionMap) Axis(action Action) float64

Axis reports the current axis value for the action.

func (*ActionMap) Bindings

func (am *ActionMap) Bindings() Bindings

Bindings returns a copy of the current bindings.

func (*ActionMap) Duration

func (am *ActionMap) Duration(action Action) float64

Duration reports how long the action has been held, in seconds.

func (*ActionMap) Held

func (am *ActionMap) Held(action Action) bool

Held reports whether the action is held in the current frame.

func (*ActionMap) JustPressed

func (am *ActionMap) JustPressed(action Action) bool

JustPressed reports whether the action was pressed in the current frame.

func (*ActionMap) JustReleased

func (am *ActionMap) JustReleased(action Action) bool

JustReleased reports whether the action was released in the current frame.

func (*ActionMap) Pressed

func (am *ActionMap) Pressed(action Action) bool

Pressed reports whether a press was latched for the current tick. It reports true for every read in the tick that consumed the press. Frame-rate code wants JustPressed instead.

func (*ActionMap) Released

func (am *ActionMap) Released(action Action) bool

Released reports whether a release was latched for the current tick. It reports true for every read in the tick that consumed the release. Frame-rate code wants JustReleased instead.

func (*ActionMap) SetBindings

func (am *ActionMap) SetBindings(bindings Bindings) error

SetBindings replaces the bindings with a copy of the argument. The new bindings take effect at the next Update.

func (*ActionMap) Tick

func (am *ActionMap) Tick()

Tick delivers the press and release events accumulated since the last tick. Call it once per fixed tick, before fixed systems read Pressed or Released.

func (*ActionMap) Update

func (am *ActionMap) Update(s *Snapshot, dt float64)

Update resolves one frame of input into per-action state. Press and release events accumulate in sinceLastTick until the next Tick delivers them. Call it once per frame, before any Tick. A nil snapshot reads as every input off.

type Bindings

type Bindings map[Action][]Input

Bindings maps Action to the physical inputs associated with that action. Each action can have multiple inputs bound to it, allowing flexible control schemes. Example:

Bindings{
    "jump":   []Input{KeyInput{KeySpace}, PadButtonInput{Button: PadSouth}},
    "move_x": []Input{KeyPairInput{Negative: KeyA, Positive: KeyD}, PadAxisInput{Axis: PadLeftStickX}},
    "p2_jump": []Input{PadButtonInput{Pad: 2, Button: PadSouth}},
}

type Input

type Input interface {
	// contains filtered or unexported methods
}

Input is the interface implemented by all bindable physical inputs. Every type that can be bound to an action implements this interface.

type Key

type Key int

Key represents a keyboard key. The provided constants cover standard alphanumeric, control, function, navigation, and numpad keys. it follows the ISO/IEC 9995 standard.

const (
	// KeyNone is the zero value of Key: no key at all. Modifier slots
	// read as empty, a binding with KeyNone never fires, and the
	// poller never reports it.
	KeyNone Key = iota
	// Alphanumeric keys
	KeyA
	KeyB
	KeyC
	KeyD
	KeyE
	KeyF
	KeyG
	KeyH
	KeyI
	KeyJ
	KeyK
	KeyL
	KeyM
	KeyN
	KeyO
	KeyP
	KeyQ
	KeyR
	KeyS
	KeyT
	KeyU
	KeyV
	KeyW
	KeyX
	KeyY
	KeyZ
	Key0
	Key1
	Key2
	Key3
	Key4
	Key5
	Key6
	Key7
	Key8
	Key9
	KeyBacktick
	KeyMinus
	KeyEqual
	KeyBracketLeft
	KeyBracketRight
	KeyBackslash
	KeySemicolon
	KeyApostrophe
	KeyComma
	KeyPeriod
	KeySlash
	// Supporting ISO/IEC 9995 standard backslash key
	KeyISOBackslash
	// Space and the control row
	KeySpace
	KeyEscape
	KeyTab
	KeyCapsLock
	KeyEnter
	KeyBackspace
	// Modifiers
	KeyShiftLeft
	KeyShiftRight
	KeyControlLeft
	KeyControlRight
	KeyAltLeft
	KeyAltRight
	// Function Row
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	// Navigation cluster
	KeyInsert
	KeyDelete
	KeyHome
	KeyEnd
	KeyPageUp
	KeyPageDown
	KeyPrintScreen
	KeyScrollLock
	KeyPause
	// Arrow keys
	KeyArrowUp
	KeyArrowDown
	KeyArrowLeft
	KeyArrowRight
	// Numpad keys
	KeyNumpad0
	KeyNumpad1
	KeyNumpad2
	KeyNumpad3
	KeyNumpad4
	KeyNumpad5
	KeyNumpad6
	KeyNumpad7
	KeyNumpad8
	KeyNumpad9
	KeyNumpadDecimal
	KeyNumpadEnter
	KeyNumpadAdd
	KeyNumpadSubtract
	KeyNumpadMultiply
	KeyNumpadDivide
	KeyNumLock
	NumKeys // sizes the number of keys
)

BASED ON ISO/IEC 9995 standard for keyboard layouts and categories

func (Key) String

func (k Key) String() string

type KeyInput

type KeyInput struct {
	// Key is the trigger: Pressed, Held, and Released follow this key
	// while every modifier is held. A Key of KeyNone never fires.
	Key Key
	// Modifiers lists keys that must be held for Key to count.
	// Empty slots (KeyNone) are ignored, so the zero value binds the
	// plain key. A modifier dropped mid-hold stops the action without
	// a release edge. A modifier equal to Key suppresses the release
	// edge: the key is no longer held on its own release frame.
	Modifiers [3]Key
}

KeyInput binds an action to a keyboard key, with optional modifier keys that must be held. Ctrl+S is KeyInput{Key: KeyS, Modifiers: [3]Key{KeyControlLeft}}.

type KeyPairInput

type KeyPairInput struct{ Negative, Positive Key }

KeyPairInput binds an axis from two keyboard keys, one for the negative direction and one for the positive direction.

type MouseButton

type MouseButton int

MouseButton represents a mouse button. The provided constants cover the standard left, right, and middle buttons.

const (
	MouseButtonLeft MouseButton = iota
	MouseButtonRight
	MouseButtonMiddle
	NumMouseButtons // sizes the number of mouse buttons
)

func (MouseButton) String

func (b MouseButton) String() string

type MouseButtonInput

type MouseButtonInput struct{ Button MouseButton }

MouseButtonInput binds an action to a mouse button.

type MouseSnapshot

type MouseSnapshot struct {
	Cursor  geom.Vector2
	Buttons [NumMouseButtons]State
	Wheel   geom.Vector2
}

MouseSnapshot represents the state of the mouse at a given moment.

type PadAxis

type PadAxis int

PadAxis identifies a gamepad axis: sticks span -1..1 with Y positive downward - the screen convention - and triggers span 0..1. The poller normalizes backend values to this contract in its device table.

const (
	PadLeftStickX PadAxis = iota
	PadLeftStickY
	PadRightStickX
	PadRightStickY
	PadLeftTrigger
	PadRightTrigger
	NumPadAxes // sizes the number of pad axes
)

func (PadAxis) String

func (a PadAxis) String() string

type PadAxisInput

type PadAxisInput struct {
	// Pad is the player number: 0 - the zero value - means any
	// connected pad, 1 the first pad, 2 the second.
	Pad int
	// Axis is the gamepad axis that triggers the action.
	Axis PadAxis
	// Direction gates which deflection drives the binding: 0 -
	// the zero value - either direction, -1 only the negative half
	// (screen-up for a Y axis), +1 only the positive half.
	Direction int
}

PadAxisInput binds an action to a gamepad axis.

type PadButton

type PadButton int

PadButton identifies a gamepad button by the standard layout's position names - south/east/west/north, not vendor glyphs (A/B/X/Y) - so bindings survive whatever controller is plugged in. The triggers are axes, not buttons (see PadAxis).

const (
	PadSouth PadButton = iota
	PadEast
	PadWest
	PadNorth
	PadLeftShoulder
	PadRightShoulder
	PadLeftStick
	PadRightStick
	PadBack
	PadStart
	PadGuide
	PadDPadUp
	PadDPadDown
	PadDPadLeft
	PadDPadRight
	NumPadButtons // sizes the number of pad buttons
)

func (PadButton) String

func (b PadButton) String() string

type PadButtonInput

type PadButtonInput struct {
	// Pad is the player number: 0 - the zero value - means any
	// connected pad, 1 the first pad, 2 the second.
	Pad int
	// Button is the gamepad button that triggers the action.
	Button PadButton
}

PadButtonInput binds an action to a gamepad button.

type PadSnapshot

type PadSnapshot struct {
	Connected bool
	Buttons   [NumPadButtons]State
	Axes      [NumPadAxes]float64
}

PadSnapshot represents the state of a gamepad at a given moment.

type Snapshot

type Snapshot struct {
	// Keys holds the state of all keyboard keys in this snapshot.
	Keys [NumKeys]State
	// Mouse holds the state of the mouse in this snapshot.
	Mouse MouseSnapshot
	// Pads holds the state of all gamepads in this snapshot.
	Pads [MaxPads]PadSnapshot
}

Snapshot is the state of all input devices at a given moment: one frame of device truth, as the runner's poller filled it. The poller writes the fields; game systems read them and treat the snapshot as read-only.

The read methods are nil-safe - a nil *Snapshot reads as no input - and bounds-safe: an out-of-range key, button, or pad index reads as zero.

func (*Snapshot) Cursor

func (s *Snapshot) Cursor() geom.Vector2

Cursor returns the current position of the mouse cursor in this snapshot.

func (*Snapshot) KeyHeld

func (s *Snapshot) KeyHeld(k Key) bool

KeyHeld reports whether the specified key is held in this snapshot.

func (*Snapshot) KeyPressed

func (s *Snapshot) KeyPressed(k Key) bool

KeyPressed reports whether the specified key was pressed in this snapshot.

func (*Snapshot) KeyReleased

func (s *Snapshot) KeyReleased(k Key) bool

KeyReleased reports whether the specified key was released in this snapshot.

func (*Snapshot) MouseHeld

func (s *Snapshot) MouseHeld(b MouseButton) bool

MouseHeld reports whether the specified mouse button is held in this snapshot.

func (*Snapshot) MousePressed

func (s *Snapshot) MousePressed(b MouseButton) bool

MousePressed reports whether the specified mouse button was pressed in this snapshot.

func (*Snapshot) MouseReleased

func (s *Snapshot) MouseReleased(b MouseButton) bool

MouseReleased reports whether the specified mouse button was released in this snapshot.

func (*Snapshot) PadAxis

func (s *Snapshot) PadAxis(padIndex int, axis PadAxis) float64

PadAxis returns the current value of the specified gamepad axis in this snapshot.

func (*Snapshot) PadConnected

func (s *Snapshot) PadConnected(padIndex int) bool

PadConnected reports whether the specified gamepad is connected in this snapshot.

func (*Snapshot) PadHeld

func (s *Snapshot) PadHeld(padIndex int, button PadButton) bool

PadHeld reports whether the specified gamepad button is held in this snapshot.

func (*Snapshot) PadPressed

func (s *Snapshot) PadPressed(padIndex int, button PadButton) bool

PadPressed reports whether the specified gamepad button was pressed in this snapshot.

func (*Snapshot) PadReleased

func (s *Snapshot) PadReleased(padIndex int, button PadButton) bool

PadReleased reports whether the specified gamepad button was released in this snapshot.

func (*Snapshot) Wheel

func (s *Snapshot) Wheel() geom.Vector2

Wheel returns the current state of the mouse wheel in this snapshot.

type State

type State struct{ Pressed, Held, Released bool }

State is one frame of one physical input: Pressed and Released are this-frame edges, Held is the level. Snapshots carry one per input; duration is an action-level accumulation, not stored here.

Jump to

Keyboard shortcuts

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