gamepad

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 14 Imported by: 2 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

Overview

Package gamepad reads the buttons and axes of connected game controllers in the W3C Standard Gamepad layout on every target that supports controllers (macOS, iOS, Windows, Linux, Android and the web; see core.Env.SupportsGamepads).

Pads is a tracked reactive read that rerenders scopes reading it when a controller connects or disconnects without rerendering on every button press or stick movement. State returns one controller's buttons and axes as of the current display frame, intended for core.Scope.RequestAnimationFrame loops.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureInput

func CaptureInput(cx *core.Scope) (release func())

CaptureInput claims raw gamepad input for the lifetime of cx (or until the returned release function is called). While any scope holds CaptureInput, EnableFocusNavigation pauses completely so active gameplay views receive raw controller input through Pads and State without moving native focus. Multiple mounted scopes may hold captures concurrently; focus navigation resumes once every capture is released.

func EnableFocusNavigation

func EnableFocusNavigation(cx *core.Scope, opts ...FocusNavOption) (stop func())

EnableFocusNavigation turns on explicit gamepad navigation through native focus stops in cx's tree. An app opts into it explicitly; nothing enables it implicitly by device.

While enabled, at least one gamepad is connected, and no scope holds CaptureInput, EnableFocusNavigation polls connected pads on RequestAnimationFrame:

  • D-pad directions and the left analog stick (past the deadzone, default 0.5, with initial/repeat delay when held) move focus across the same enabled native focus stops Tab reaches.
  • ButtonSouth (A / Cross) activates the currently focused native view.
  • ButtonEast (B / Circle) triggers Back (overlays, modals, Scope.OnBackPress, navigation stacks).

When no pads are connected or while any mounted scope holds CaptureInput, the animation frame loop stops so no idle frames run. Navigation stops automatically when cx is disposed or when the returned stop function is called.

func Rumble

func Rumble(cx *core.Scope, id int, lowFreq, highFreq float32, duration time.Duration)

Rumble drives controller id's low-frequency (left/heavy) and high-frequency (right/light) rumble motors for duration, replacing any running rumble on that controller. Intensities are clamped to [0, 1] (NaN becomes 0).

A controller that is disconnected or has CanRumble == false does nothing. Zero intensities or a non-positive duration stops any running rumble. Rumble stops automatically after duration, when the app enters the background or loses focus (cx.Env().Lifecycle() != core.LifecycleActive), when cx is disposed, or when the controller disconnects.

func StopRumble

func StopRumble(cx *core.Scope, id int)

StopRumble stops any running rumble on controller id. A controller that is disconnected or has CanRumble == false does nothing.

Types

type Axis

type Axis uint8

Axis is an analog stick axis index in the W3C Standard Gamepad layout (0..3). Negative values point left or up; positive values point right or down, in [-1, 1].

const (
	AxisLeftX  Axis = 0
	AxisLeftY  Axis = 1
	AxisRightX Axis = 2
	AxisRightY Axis = 3

	AxisCount = 4
)

W3C Standard Gamepad analog stick axis indices.

type Button

type Button uint8

Button is a button index in the W3C Standard Gamepad layout (0..16).

const (
	ButtonSouth      Button = 0 // Bottom face button (Xbox A, PlayStation Cross, Nintendo B)
	ButtonA                 = ButtonSouth
	ButtonEast       Button = 1 // Right face button (Xbox B, PlayStation Circle, Nintendo A)
	ButtonB                 = ButtonEast
	ButtonWest       Button = 2 // Left face button (Xbox X, PlayStation Square, Nintendo Y)
	ButtonX                 = ButtonWest
	ButtonNorth      Button = 3 // Top face button (Xbox Y, PlayStation Triangle, Nintendo X)
	ButtonY                 = ButtonNorth
	ButtonLB         Button = 4 // Left shoulder / bumper (L1)
	ButtonRB         Button = 5 // Right shoulder / bumper (R1)
	ButtonLT         Button = 6 // Left trigger (L2), analog in [0, 1]
	ButtonRT         Button = 7 // Right trigger (R2), analog in [0, 1]
	ButtonBack       Button = 8 // Left center button (Back, Select, Share, View)
	ButtonSelect            = ButtonBack
	ButtonStart      Button = 9  // Right center button (Start, Options, Menu)
	ButtonLeftStick  Button = 10 // Left stick click (L3)
	ButtonRightStick Button = 11 // Right stick click (R3)
	ButtonDPadUp     Button = 12
	ButtonDPadDown   Button = 13
	ButtonDPadLeft   Button = 14
	ButtonDPadRight  Button = 15
	ButtonHome       Button = 16 // Center logo / guide / Home button

	ButtonCount = 17
)

W3C Standard Gamepad button indices.

type Family

type Family uint8

Family identifies a controller's physical button-label family so games can show matching on-screen prompts (Xbox A/B/X/Y, PlayStation Cross/Circle/Square/Triangle, Nintendo B/A/Y/X, or a generic fallback).

func DetectFamily

func DetectFamily(vendorID, productID uint16, name string) Family

DetectFamily classifies a controller from its USB vendor/product IDs and reported name or product category string. First-party USB vendor IDs take precedence, followed by controller name patterns, known third-party vendor IDs, and finally FamilyGeneric.

func (Family) String

func (f Family) String() string

String returns the family's lowercase name: generic, xbox, playstation or nintendo.

type FocusNavOption

type FocusNavOption func(*focusNavConfig)

FocusNavOption configures gamepad focus navigation (EnableFocusNavigation).

func WithDeadzone

func WithDeadzone(threshold float32) FocusNavOption

WithDeadzone sets the left-stick axis threshold in (0, 1] beyond which stick deflection moves focus. The default is 0.5.

func WithRepeatDelay

func WithRepeatDelay(initial, interval time.Duration) FocusNavOption

WithRepeatDelay sets the initial delay before a held D-pad or left-stick direction begins repeating and the interval between subsequent repeats. The defaults are 400ms and 120ms.

func WithWrap

func WithWrap(wrap bool) FocusNavOption

WithWrap controls whether directional moves wrap in Tab order when no focus stop lies further in that direction. The default is false.

type Pad

type Pad struct {
	ID        int
	Name      string
	VendorID  uint16
	ProductID uint16
	Family    Family
	CanRumble bool
	Buttons   [ButtonCount]float32
	Axes      [AxisCount]float32
}

Pad is a connected game controller's state in the W3C Standard Gamepad layout.

func Pads

func Pads(cx *core.Scope) []Pad

Pads returns the currently connected game controllers, sorted by ID. It is a tracked reactive read: scopes reading it rerender when controllers connect or disconnect, but not when buttons or axes change. When cx's core.Access refuses core.AccessGamepad, Pads returns nil.

func State

func State(cx *core.Scope, id int) (Pad, bool)

State returns controller id's buttons and axes as of the current display frame, or (Pad{}, false) if no controller with that ID is connected or cx's core.Access refuses core.AccessGamepad. During a core.Scope.RequestAnimationFrame pass all calls observe the same frame snapshot; outside a display frame State polls the current values.

func (Pad) Pressed

func (p Pad) Pressed(b Button) bool

Pressed reports whether button b is pressed (value >= 0.5).

type RumbleEvent

type RumbleEvent struct {
	ID       int
	LowFreq  float32
	HighFreq float32
	Duration time.Duration
	Stopped  bool
}

RumbleEvent records one rumble command or stop sent to a controller.

Jump to

Keyboard shortcuts

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