gamepad

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 9 Imported by: 1 module, 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 game controllers: their buttons, triggers and sticks in the standard layout of an Xbox controller, which the W3C's Gamepad API and SDL's game controller database name, whatever pad it is:

for _, pad := range gamepad.Pads() {
	s := pad.State()
	x, y := s.LeftStick()
	if s.Pressed(gamepad.South) {
		jump()
	}
}

A game reads the pads' states once a frame, or once a step of its world, and keeps the last to tell a button just pressed from one held (State.JustPressed).

The pads come from GameController.framework on macOS (Xbox, PlayStation, Switch Pro and MFi controllers), XInput on Windows (Xbox controllers and those that act as one, as Steam Input makes of others), evdev on Linux (/dev/input, readable by the user at the seat), and the Gamepad API in browsers. On macOS, input reaches the app in front only, as games expect. Every function and method is safe to call from any goroutine.

Index

Constants

View Source
const Deadzone = 0.15

Deadzone is how far from the middle a stick must move to count, as a fraction of its reach: sticks rest a little off their middle.

Variables

This section is empty.

Functions

This section is empty.

Types

type Axis

type Axis uint8

Axis is an axis of the sticks, from -1 to 1: right and down are positive, as on the screen.

const (
	LeftX Axis = iota
	LeftY
	RightX
	RightY
	AxisCount
)

The axes.

type Button

type Button uint8

Button is a button of the standard layout, named by place: South is A on an Xbox controller, Cross on a PlayStation one, B on a Nintendo one.

const (
	South Button = iota
	East
	West
	North
	LeftShoulder
	RightShoulder
	LeftTrigger // analog: Value is from 0 to 1
	RightTrigger
	Back // View, Select, Share, Minus
	Start
	LeftStickButton
	RightStickButton
	Up
	Down
	Left
	Right
	Guide // the Xbox, PlayStation or Home button, where the system leaves it
	ButtonCount
)

The buttons, in the order of the W3C's standard gamepad.

type Pad

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

A Pad is a game controller, from when it connects until it disconnects; connected again, it is a Pad anew.

func Pads

func Pads() []*Pad

Pads returns the pads connected, in the order they connected. It looks for pads connected since at most once a second.

func (*Pad) Connected

func (p *Pad) Connected() bool

Connected reports whether the pad is still connected.

func (*Pad) Name

func (p *Pad) Name() string

Name returns the pad's name, as its maker gives it.

func (*Pad) State

func (p *Pad) State() State

State reads the pad's controls now; a pad no longer connected reads nothing pressed.

type State

type State struct {
	// Buttons are from 0 to 1: 0 or 1 but for the triggers.
	Buttons [ButtonCount]float32
	// Axes are from -1 to 1, as the pad reports them, without a dead
	// zone.
	Axes [AxisCount]float32
}

State is what a pad's controls read at one time.

func (State) Axis

func (s State) Axis(a Axis) float32

Axis returns the raw value of a, from -1 to 1.

func (State) JustPressed

func (s State) JustPressed(last State, b Button) bool

JustPressed reports whether b is pressed in s and was not in last, the state read before.

func (State) JustReleased

func (s State) JustReleased(last State, b Button) bool

JustReleased reports whether b was pressed in last and is not in s.

func (State) LeftStick

func (s State) LeftStick() (x, y float32)

LeftStick returns where the left stick is, each coordinate from -1 to 1, right and down positive, with a radial dead zone: 0 near the middle, then from 0 at Deadzone to 1 at the rim, the same in every direction.

func (State) Pressed

func (s State) Pressed(b Button) bool

Pressed reports whether b is pressed: a trigger a quarter of the way.

func (State) RightStick

func (s State) RightStick() (x, y float32)

RightStick returns where the right stick is, as LeftStick.

func (State) Value

func (s State) Value(b Button) float32

Value returns how far b is pressed, from 0 to 1.

Jump to

Keyboard shortcuts

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