gamepad

package
v1.17.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: BSD-3-Clause Imports: 9 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

Overview

Package gamepad reads game controllers: which pads are connected, the buttons and axes of each in one standard layout (SPEC §28.2), their kind (Xbox, PlayStation, Switch, ...), and rumble and lights where the platform has them.

A pad's buttons are the same on every pad, whatever it reports: the mapping of the pad's own numbers to the standard layout comes from SDL 3's table of mappings and from the Linux Gamepad Specification, and an app can add mappings of its own in the community's format with Gamepads.AddMappings. No dead zone is applied to the axes: that is the app's choice, and ApplyDeadZone helps with it.

By default the pads also move the focus of the app and press its buttons, as key events: see Gamepads.SetKeyEvents.

Everything here belongs to the UI thread. The package is part of Glutter's port of SDL 3's gamepad API (SPEC §28.1); the mapping format and the table are SDL's (zlib license, third_party/sdl/LICENSE).

Flutter: no counterpart

Index

Constants

View Source
const (
	// South is the bottom face button.
	//
	// Flutter: no counterpart
	South = Button(gamepaddb.South)
	// East is the right face button.
	//
	// Flutter: no counterpart
	East = Button(gamepaddb.East)
	// West is the left face button.
	//
	// Flutter: no counterpart
	West = Button(gamepaddb.West)
	// North is the top face button.
	//
	// Flutter: no counterpart
	North = Button(gamepaddb.North)
	// Back is the button on the left of the middle (View, Select, Share).
	//
	// Flutter: no counterpart
	Back = Button(gamepaddb.Back)
	// Guide is the button in the middle (Xbox, PS, Home). Not every platform gives it to apps.
	//
	// Flutter: no counterpart
	Guide = Button(gamepaddb.Guide)
	// Start is the button on the right of the middle (Menu, Options).
	//
	// Flutter: no counterpart
	Start = Button(gamepaddb.Start)
	// LeftStick is the press of the left stick.
	//
	// Flutter: no counterpart
	LeftStick = Button(gamepaddb.LeftStick)
	// RightStick is the press of the right stick.
	//
	// Flutter: no counterpart
	RightStick = Button(gamepaddb.RightStick)
	// LeftShoulder is the left bumper.
	//
	// Flutter: no counterpart
	LeftShoulder = Button(gamepaddb.LeftShoulder)
	// RightShoulder is the right bumper.
	//
	// Flutter: no counterpart
	RightShoulder = Button(gamepaddb.RightShoulder)
	// DPadUp is up on the D-pad.
	//
	// Flutter: no counterpart
	DPadUp = Button(gamepaddb.DPadUp)
	// DPadDown is down on the D-pad.
	//
	// Flutter: no counterpart
	DPadDown = Button(gamepaddb.DPadDown)
	// DPadLeft is left on the D-pad.
	//
	// Flutter: no counterpart
	DPadLeft = Button(gamepaddb.DPadLeft)
	// DPadRight is right on the D-pad.
	//
	// Flutter: no counterpart
	DPadRight = Button(gamepaddb.DPadRight)
	// Misc1 is an extra button: Share on an Xbox Series pad, the microphone button on a DualSense.
	//
	// Flutter: no counterpart
	Misc1 = Button(gamepaddb.Misc1)
	// Paddle1 is the upper right paddle on the back of the pad.
	//
	// Flutter: no counterpart
	Paddle1 = Button(gamepaddb.Paddle1)
	// Paddle2 is the upper left paddle on the back.
	//
	// Flutter: no counterpart
	Paddle2 = Button(gamepaddb.Paddle2)
	// Paddle3 is the lower right paddle on the back.
	//
	// Flutter: no counterpart
	Paddle3 = Button(gamepaddb.Paddle3)
	// Paddle4 is the lower left paddle on the back.
	//
	// Flutter: no counterpart
	Paddle4 = Button(gamepaddb.Paddle4)
	// Touchpad is the press of the touchpad of a PlayStation pad.
	//
	// Flutter: no counterpart
	Touchpad = Button(gamepaddb.Touchpad)
	// Misc2 is another extra button.
	//
	// Flutter: no counterpart
	Misc2 = Button(gamepaddb.Misc2)
	// Misc3 is another extra button.
	//
	// Flutter: no counterpart
	Misc3 = Button(gamepaddb.Misc3)
	// Misc4 is another extra button.
	//
	// Flutter: no counterpart
	Misc4 = Button(gamepaddb.Misc4)
	// Misc5 is another extra button.
	//
	// Flutter: no counterpart
	Misc5 = Button(gamepaddb.Misc5)
	// Misc6 is another extra button.
	//
	// Flutter: no counterpart
	Misc6 = Button(gamepaddb.Misc6)
)

The buttons of the standard layout. South, East, West and North are the positions of the four face buttons: A, B, X and Y on an Xbox pad, cross, circle, square and triangle on a PlayStation one (see Pad.FaceStyle).

View Source
const (
	// LeftX is the left stick from left (−1) to right (1).
	//
	// Flutter: no counterpart
	LeftX = Axis(gamepaddb.LeftX)
	// LeftY is the left stick from up (−1) to down (1).
	//
	// Flutter: no counterpart
	LeftY = Axis(gamepaddb.LeftY)
	// RightX is the right stick from left (−1) to right (1).
	//
	// Flutter: no counterpart
	RightX = Axis(gamepaddb.RightX)
	// RightY is the right stick from up (−1) to down (1).
	//
	// Flutter: no counterpart
	RightY = Axis(gamepaddb.RightY)
	// LeftTrigger is the left trigger from 0 to 1.
	//
	// Flutter: no counterpart
	LeftTrigger = Axis(gamepaddb.LeftTrigger)
	// RightTrigger is the right trigger from 0 to 1.
	//
	// Flutter: no counterpart
	RightTrigger = Axis(gamepaddb.RightTrigger)
)

The axes of the standard layout.

View Source
const (
	// KindGeneric is a pad that is not known by model, or has the standard layout.
	//
	// Flutter: no counterpart
	KindGeneric = PadKind(iota)
	// KindXbox360 is an Xbox 360 pad.
	//
	// Flutter: no counterpart
	KindXbox360
	// KindXboxOne is an Xbox One, Xbox Series or Xbox Elite pad.
	//
	// Flutter: no counterpart
	KindXboxOne
	// KindPlayStation3 is a PlayStation 3 pad.
	//
	// Flutter: no counterpart
	KindPlayStation3
	// KindPlayStation4 is a PlayStation 4 pad (DualShock 4).
	//
	// Flutter: no counterpart
	KindPlayStation4
	// KindPlayStation5 is a PlayStation 5 pad (DualSense).
	//
	// Flutter: no counterpart
	KindPlayStation5
	// KindSwitchPro is a Nintendo Switch Pro pad, or a pad that copies it.
	//
	// Flutter: no counterpart
	KindSwitchPro
	// KindJoyConLeft is a left Joy-Con on its own.
	//
	// Flutter: no counterpart
	KindJoyConLeft
	// KindJoyConRight is a right Joy-Con on its own.
	//
	// Flutter: no counterpart
	KindJoyConRight
	// KindJoyConPair is two Joy-Cons used as one pad.
	//
	// Flutter: no counterpart
	KindJoyConPair
	// KindGameCube is a GameCube pad.
	//
	// Flutter: no counterpart
	KindGameCube
	// KindSteam is a Steam pad.
	//
	// Flutter: no counterpart
	KindSteam
)

The kinds of pads.

View Source
const (
	// FaceABXY is A at the bottom, B on the right, X on the left and Y at the top (Xbox).
	//
	// Flutter: no counterpart
	FaceABXY = FaceStyle(iota)
	// FaceAXBY is A at the bottom, X on the right, B on the left and Y at the top (GameCube).
	//
	// Flutter: no counterpart
	FaceAXBY
	// FaceBAYX is B at the bottom, A on the right, Y on the left and X at the top (Nintendo).
	//
	// Flutter: no counterpart
	FaceBAYX
	// FaceSony is cross at the bottom, circle on the right, square on the left and triangle at the top (PlayStation).
	//
	// Flutter: no counterpart
	FaceSony
)

The styles of face buttons.

View Source
const (
	// ButtonDown is a button that went down.
	//
	// Flutter: no counterpart
	ButtonDown = EventKind(pads.ButtonDown)
	// ButtonUp is a button that went up.
	//
	// Flutter: no counterpart
	ButtonUp = EventKind(pads.ButtonUp)
	// AxisMotion is an axis that has a new value.
	//
	// Flutter: no counterpart
	AxisMotion = EventKind(pads.AxisMotion)
)
View Source
const (
	// TagMappingInvalid is the tag of the error of a mapping line that cannot be read.
	//
	// Flutter: no counterpart
	TagMappingInvalid = gamepaddb.TagMappingInvalid
	// TagRumbleUnavailable is the tag of the error of a rumble that the pad or the platform cannot make.
	//
	// Flutter: no counterpart
	TagRumbleUnavailable = "GamepadRumbleUnavailable"
	// TagLEDUnavailable is the tag of the error of a light that the pad or the platform cannot set.
	//
	// Flutter: no counterpart
	TagLEDUnavailable = "GamepadLEDUnavailable"
)

The tags of the errors of this package (SPEC §28.7).

View Source
const AxisCount = int(gamepaddb.AxisCount)

AxisCount is the number of axes; the axes are 0 to AxisCount-1.

Flutter: no counterpart

View Source
const ButtonCount = int(gamepaddb.ButtonCount)

ButtonCount is the number of buttons; the buttons are 0 to ButtonCount-1.

Flutter: no counterpart

Variables

This section is empty.

Functions

func ApplyDeadZone

func ApplyDeadZone(v, dead float64) float64

ApplyDeadZone turns the value of one axis into one with a dead zone: a value whose size is under dead is 0, and the rest is spread over the full range, so that the value is continuous. dead is from 0 up to, not including, 1.

Flutter: no counterpart

func ApplyStickDeadZone

func ApplyStickDeadZone(x, y, dead float64) (float64, float64)

ApplyStickDeadZone does the same for the two axes of a stick together: the dead zone is a circle, so that a stick that is pushed in a diagonal is not cut by a square, and the size of the push is spread over the full range with the direction kept. The result is not longer than 1.

Flutter: no counterpart

Types

type Axis

type Axis uint8

Axis is an axis of the standard layout.

Flutter: no counterpart

func (Axis) String

func (a Axis) String() string

String is the name of the axis as a mapping spells it ("lefttrigger").

Flutter: no counterpart

type Battery

type Battery struct {
	// Known says whether the platform knows the charge; Level is from 0 to 1 and means nothing when Known is false.
	Known bool
	Level float64
	// Charging says that the battery is being charged, and Charged that it is full while the pad is plugged in. A pad that does
	// not say has neither.
	Charging, Charged bool
}

Battery is the charge of a pad.

Flutter: no counterpart

type Button

type Button uint8

Button is a button of the standard layout.

Flutter: no counterpart

func (Button) String

func (b Button) String() string

String is the name of the button as a mapping spells it ("leftshoulder").

Flutter: no counterpart

type Event

type Event struct {
	// Pad is the pad that made the event.
	Pad *Pad
	// Time is when the pad reported the change, on the system's monotonic clock.
	Time time.Duration
	Kind EventKind
	// Button is the button of a ButtonDown or ButtonUp.
	Button Button
	// Axis and Value are the axis of an AxisMotion and its new value (see State.Axis).
	Axis  Axis
	Value float64
}

Event is a change of a button or an axis of a pad. Events reach the listeners on the UI thread, in the order they happened, with the time the pad reported them.

Flutter: no counterpart

type EventKind

type EventKind uint8

EventKind says what an Event reports.

Flutter: no counterpart

type FaceStyle

type FaceStyle uint8

FaceStyle is how the four face buttons of a pad are labelled: South, East, West and North are A, B, X and Y on an Xbox pad and cross, circle, square and triangle on a PlayStation one.

Flutter: no counterpart

type Gamepads

type Gamepads struct {
	foundation.ChangeNotifier
	// contains filtered or unexported fields
}

Gamepads is the set of the pads of the app. It is a ChangeNotifier: its listeners are called when a pad is connected or removed. Get it with Of.

Flutter: no counterpart

func ForBinding

func ForBinding(b *widgets.WidgetsBinding) *Gamepads

ForBinding returns the gamepads of the binding, for code that has no BuildContext.

Flutter: no counterpart

func Of

Of returns the gamepads of the app (the ones of the binding). An app on a platform that has no gamepad service gets a set that never has a pad.

Flutter: no counterpart

func (*Gamepads) AddMappings

func (g *Gamepads) AddMappings(text string) (int, error)

AddMappings adds mappings in the community's format (the lines of SDL's gamecontrollerdb.txt: "GUID,name,a:b0,b:b1,..., platform:Linux"). The lines for another platform are left out, and a mapping for a pad that has one replaces it. A pad that had no mapping becomes a gamepad if one of the lines is for it. It returns the number of mappings that are new, and fails with GamepadMappingInvalid (TagMappingInvalid) at the first line that cannot be read; the lines before it are added.

Flutter: no counterpart

func (*Gamepads) AppendPads

func (g *Gamepads) AppendPads(dst []*Pad) []*Pad

AppendPads appends the connected pads that have a mapping, in the order they were connected, to dst and returns the extended slice, as Pads would give them, but with no allocation when dst has room: a game that reads its pads at every frame keeps one slice and calls dst = AppendPads(dst[:0]).

Flutter: no counterpart

func (*Gamepads) Available

func (g *Gamepads) Available() bool

Available says whether the platform has a gamepad service at all.

Flutter: no counterpart

func (*Gamepads) KeyEvents

func (g *Gamepads) KeyEvents() bool

KeyEvents says whether the pads make key events.

Flutter: no counterpart

func (*Gamepads) Pads

func (g *Gamepads) Pads() []*Pad

Pads returns the connected pads that have a mapping, in the order they were connected. The slice is a copy.

Flutter: no counterpart

func (*Gamepads) SetKeyEvents

func (g *Gamepads) SetKeyEvents(on bool)

SetKeyEvents turns on or off that the pads make key events: the buttons as the game-button keys (South as GameButtonA, East as GameButtonB, West as GameButtonX, North as GameButtonY, the shoulders as GameButtonLeft1 and GameButtonRight1, the triggers as GameButtonLeft2 and GameButtonRight2 from half their travel, Start, Back and Guide as GameButtonStart, GameButtonSelect and GameButtonMode, the sticks' presses as GameButtonThumbLeft and GameButtonThumbRight) and the D-pad as the arrow keys. They are on at first: they give every app focus traversal and activation with a pad (ActivateIntent on GameButtonA). Flutter does this on Android only (SPEC §12.10). Only the window that has the focus gets the keys.

Flutter: no counterpart

type Pad

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

Pad is a connected gamepad. A pad has the same value for as long as it is connected.

Flutter: no counterpart

func (*Pad) AddListener

func (p *Pad) AddListener(fn func(Event)) (remove func())

AddListener adds a function that is called, on the UI thread, with every change of a button or axis of the pad, and returns the function that removes it. A listener may add and remove listeners.

Flutter: no counterpart

func (*Pad) AddTouchListener

func (p *Pad) AddTouchListener(fn func(Touch)) (remove func())

AddTouchListener adds a function that is called, on the UI thread, when a finger goes down, moves or goes up on a touchpad of the pad, and returns the function that removes it. A listener may add and remove listeners.

Flutter: no counterpart

func (*Pad) Battery

func (p *Pad) Battery() Battery

Battery returns the charge of the pad.

Flutter: no counterpart

func (*Pad) CanRumble

func (p *Pad) CanRumble() bool

CanRumble says whether the pad has motors that Rumble can turn.

Flutter: no counterpart

func (*Pad) CanRumbleTriggers

func (p *Pad) CanRumbleTriggers() bool

CanRumbleTriggers says whether the pad has motors in its triggers.

Flutter: no counterpart

func (*Pad) Connected

func (p *Pad) Connected() bool

Connected says whether the pad is still there.

Flutter: no counterpart

func (*Pad) FaceStyle

func (p *Pad) FaceStyle() FaceStyle

FaceStyle is how the face buttons of the pad are labelled.

Flutter: no counterpart

func (*Pad) GUID

func (p *Pad) GUID() string

GUID names the model of the pad as a mapping line does: 32 hexadecimal digits.

Flutter: no counterpart

func (*Pad) HasAxis

func (p *Pad) HasAxis(a Axis) bool

HasAxis says whether the pad has the axis.

Flutter: no counterpart

func (*Pad) HasButton

func (p *Pad) HasButton(b Button) bool

HasButton says whether the pad has the button.

Flutter: no counterpart

func (*Pad) HasLED

func (p *Pad) HasLED() bool

HasLED says whether the pad has a light that SetLED can set.

Flutter: no counterpart

func (*Pad) HasSensors

func (p *Pad) HasSensors() bool

HasSensors says whether the pad has a gyroscope and an accelerometer (the PlayStation pads and the Switch Pro controller do, when the system lets the app read them: SPEC §28.2).

Flutter: no counterpart

func (*Pad) ID

func (p *Pad) ID() PadID

ID is the platform's number of the pad.

Flutter: no counterpart

func (*Pad) Kind

func (p *Pad) Kind() PadKind

Kind is the model of the pad.

Flutter: no counterpart

func (*Pad) Name

func (p *Pad) Name() string

Name is the name of the pad.

Flutter: no counterpart

func (*Pad) OpenSensor

func (p *Pad) OpenSensor(kind sensors.Kind) (*sensors.Sensor, error)

OpenSensor opens one of the pad's sensors as a sensors.Sensor, with the axes of the pad lying flat with its sticks up (x to the right, y up, z out of its face) in m/s² and rad/s. The pad reports its sensors only while one is open, which saves its battery. It fails with GamepadSensorsUnavailable (platform.TagGamepadSensorsUnavailable) when the pad has none or the system cannot turn them on. Call it, and Close of the sensor, on the UI thread.

Flutter: no counterpart

func (*Pad) Path

func (p *Pad) Path() string

Path says where the pad is on the system ("/dev/input/event7" on Linux), or "".

Flutter: no counterpart

func (*Pad) Rumble

func (p *Pad) Rumble(low, high float64, d time.Duration) error

Rumble turns the motors of the pad for d: low is the strength of the big, slow motor and high that of the small, fast one, each from 0 to 1. A rumble that is running is replaced; 0 and 0 stop it. It fails with GamepadRumbleUnavailable (TagRumbleUnavailable) when the pad or the platform cannot rumble.

Flutter: no counterpart

func (*Pad) RumbleTriggers

func (p *Pad) RumbleTriggers(left, right float64, d time.Duration) error

RumbleTriggers turns the motors of the triggers of the pad (Xbox One and Series pads) for d, each from 0 to 1. It fails with GamepadRumbleUnavailable (TagRumbleUnavailable) when the pad or the platform cannot.

Flutter: no counterpart

func (*Pad) SensorRate

func (p *Pad) SensorRate() float64

SensorRate is how many readings a second each sensor of the pad makes, or 0 when the pad does not say.

Flutter: no counterpart

func (*Pad) SetLED

func (p *Pad) SetLED(c ui.Color) error

SetLED sets the colour of the light of the pad (the light bar of a PlayStation pad); the alpha is ignored. It fails with GamepadLEDUnavailable (TagLEDUnavailable) when the pad or the platform has none.

Flutter: no counterpart

func (*Pad) State

func (p *Pad) State() State

State returns the buttons and axes of the pad now, for reading once a frame.

Flutter: no counterpart

func (*Pad) Touches

func (p *Pad) Touches() []Touch

Touches returns the fingers that are on the touchpads of the pad now.

Flutter: no counterpart

func (*Pad) Touchpads

func (p *Pad) Touchpads() int

Touchpads is the number of touchpads of the pad: 1 for a PlayStation pad with the HID driver (SPEC §28.2), else 0. The press of the touchpad is the button Touchpad; this is for the fingers on it.

Flutter: no counterpart

type PadID

type PadID int64

PadID names a pad while it is connected. The platform makes it, and it is not used again during a run.

Flutter: no counterpart

type PadKind

type PadKind uint8

PadKind is the model of a pad, as far as its vendor and product numbers tell.

Flutter: no counterpart

func (PadKind) String

func (k PadKind) String() string

String names the kind in words.

Flutter: no counterpart

type State

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

State is the buttons and axes of a pad at one moment.

Flutter: no counterpart

func (State) Axis

func (s State) Axis(a Axis) float64

Axis returns the value of the axis: from −1 to 1 for a stick, from 0 to 1 for a trigger. No dead zone is applied.

Flutter: no counterpart

func (State) Down

func (s State) Down(b Button) bool

Down says whether the button is down.

Flutter: no counterpart

type Touch

type Touch struct {
	// Pad is the pad that has the touchpad.
	Pad *Pad
	// Time is when the pad reported the finger, on the system's monotonic clock.
	Time time.Duration
	// Touchpad and Finger number the touchpad and the finger, from 0.
	Touchpad, Finger int
	// Down says that the finger is on the touchpad; a finger that left keeps the place it left from. X and Y are from 0 to 1,
	// from the top left corner, and Pressure is 1 for a finger that is down and 0 for one that is not.
	Down           bool
	X, Y, Pressure float64
}

Touch is a finger on the touchpad of a pad: the PlayStation pads have one that takes two fingers.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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