pad

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 14 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 pad reads game controllers on every platform giokit runs on and turns them into per-frame button events.

Every controller is presented in one standard layout, the one Xbox, PlayStation, Nintendo and Steam pads share: four face buttons named by where they sit (South, East, West, North), shoulders, analog triggers, two sticks, a d-pad, and Back/Start/Guide. A game binds positions, not letters, so South is "A" on an Xbox pad, "✕" on a PlayStation pad and "B" on a Nintendo pad (Pads.NintendoConfirm instead makes South the A button there, so it confirms on every pad); Label names a button the way the player's pad prints it.

Sources:

  • Linux: evdev (/dev/input/event*), with hot-plug. The kernel drivers for Xbox, PlayStation and Switch pads already report this layout.
  • Windows: XInput (Xbox pads, and anything Steam Input presents as one).
  • macOS: the GameController framework (needs cgo).
  • Web: the Gamepad API, with the "standard" mapping.

Steam Input needs nothing extra: a game launched through Steam sees any controller as a virtual Xbox pad, which every source reads.

Use one Pads per window. Call Frame at the top of every frame, then read Events or run a Map:

pads := pad.New(app) // once; app wakes the UI when a pad changes

func (v *view) Frame(gtx layout.Context) {
	v.pads.Frame(gtx)
	v.padKeys.Run(gtx, v.pads)
	...
}

Index

Constants

View Source
const (
	RepeatDelay    = 350 * time.Millisecond
	RepeatInterval = 90 * time.Millisecond
)

Repeat timing for held buttons, like a held key.

Variables

View Source
var Debug = os.Getenv("GIOKIT_PAD_DEBUG") != ""

Debug makes sources log the devices they consider and every raw event to stderr (Linux), for working out why a pad doesn't respond. Set it before the first Frame; GIOKIT_PAD_DEBUG=1 sets it too.

Functions

func ASCIILabel

func ASCIILabel(f Family, b Button) string

ASCIILabel is Label without symbols, for fonts that lack ✕○□△ and ←: the PlayStation face buttons become "CROSS", "CIRCLE", "SQUARE", "TRIANGLE".

func Label

func Label(f Family, b Button) string

Label is how a pad of family f prints button b: "A" (Xbox), "✕" (PlayStation), "B" (Nintendo) for South. Generic and Steam pads use the Xbox names.

func Nav(gtx layout.Context, p *Pads, back func())

Nav drives keyboard focus from the pad, so any focusable UI (menus, dialogs, forms) works without a keyboard: the d-pad (and left stick) move focus by geometry, South clicks the focused control, and East runs back, if not nil (close the menu or dialog). It consumes those events, so run it before Maps that use the same buttons, and only while that UI is up. A control "clicks" when the focused tag has a Click method, as kit.Clickable does.

Types

type Axis

type Axis uint8

Axis is an analog control.

const (
	LeftX            Axis = iota // -1 left .. 1 right
	LeftY                        // -1 up .. 1 down
	RightX                       // -1 left .. 1 right
	RightY                       // -1 up .. 1 down
	LeftTriggerAxis              // 0 released .. 1 fully pressed
	RightTriggerAxis             // 0 released .. 1 fully pressed
	AxisCount
)

type Binding

type Binding struct {
	Button Button
	Title  string // human description, for help screens
	Run    func()
	// Repeat runs it again while the button is held, like a held key:
	// for moving a cursor or focus.
	Repeat bool
	// contains filtered or unexported fields
}

Binding is one button → function.

type Button

type Button uint8

Button is a control in the standard layout.

const (
	South Button = iota // bottom face button: A, ✕, B (Nintendo)
	East                // right face button: B, ○, A (Nintendo)
	West                // left face button: X, □, Y (Nintendo)
	North               // top face button: Y, △, X (Nintendo)
	LeftShoulder
	RightShoulder
	LeftTrigger // also an axis; pressed past half travel
	RightTrigger
	Back  // View, Share/Create, −
	Start // Menu, Options, +
	LeftStick
	RightStick
	Up // d-pad; the left stick also presses these (see Pads.StickAsDPad)
	Down
	Left
	Right
	Guide // Xbox, PS, Home; often taken by the system
	ButtonCount
)

type Buttons

type Buttons uint32

Buttons is a set of buttons.

func (Buttons) Has

func (s Buttons) Has(b Button) bool

Has reports whether b is in the set.

func (Buttons) With

func (s Buttons) With(b Button) Buttons

With returns the set with b added.

type Event

type Event struct {
	Pad    int // Pad.ID
	Family Family
	Button Button
	Kind   EventKind
}

Event is a button change on one pad.

type EventKind

type EventKind uint8

EventKind says what happened to a button.

const (
	Press   EventKind = iota // the button went down
	Release                  // the button came up
	Repeat                   // the button is still held: auto-repeat, like a held key
)

type Fake

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

Fake is a Source for tests: pads appear, press and move only when told. Its methods are safe from any goroutine. The zero value has no pads.

func (*Fake) Close

func (f *Fake) Close() error

Close implements Source.

func (*Fake) Connect

func (f *Fake) Connect(name string, family Family) int

Connect adds a pad and returns its id.

func (*Fake) Disconnect

func (f *Fake) Disconnect(id int)

Disconnect removes pad id.

func (*Fake) Poll

func (f *Fake) Poll(dst []Pad) []Pad

Poll implements Source.

func (*Fake) Set

func (f *Fake) Set(id int, b Button, down bool)

Set holds (down) or releases b on pad id. The change shows at the next Pads.Frame.

func (*Fake) SetAxis

func (f *Fake) SetAxis(id int, a Axis, v float32)

SetAxis sets axis a on pad id.

func (*Fake) Start

func (f *Fake) Start(wake func()) error

Start implements Source.

type Family

type Family uint8

Family is the kind of controller, for labels and glyphs.

const (
	Generic Family = iota // unknown; labelled like an Xbox pad
	Xbox
	PlayStation
	Nintendo
	Steam
)

type Host

type Host interface{ Invalidate() }

Host is what Pads needs from the app: kit.App is one. Invalidate requests a frame from any goroutine. If the host also has Focused() bool, input is ignored while it reports false, so a background window doesn't react.

type Map

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

Map binds buttons to functions, the gamepad counterpart of kit.Keymap. Bind once, then Run it every frame where it applies. Maps run in order: an event one Map handled is consumed, so run the most specific first (an open menu's before the game's). The zero value is an empty map. A Map belongs to the UI goroutine, and its functions run there.

func (*Map) Bind

func (m *Map) Bind(b Button, title string, f func())

Bind runs f when b is pressed.

func (*Map) BindIf

func (m *Map) BindIf(b Button, title string, enabled func() bool, f func())

BindIf is Bind for a binding that only applies while enabled reports true; otherwise the press passes on to later Maps, as if unbound.

func (*Map) BindRepeat

func (m *Map) BindRepeat(b Button, title string, f func())

BindRepeat runs f when b is pressed and again while it is held.

func (*Map) Bindings

func (m *Map) Bindings() []Binding

Bindings returns every binding, in bind order, for a help screen.

func (*Map) Run

func (m *Map) Run(gtx layout.Context, p *Pads)

Run handles this frame's unconsumed presses (and repeats, for Repeat bindings), running the first live binding for each and consuming it.

type Pad

type Pad struct {
	// ID identifies the pad while it stays connected; a reconnected pad
	// may get a new one.
	ID      int
	Name    string
	Family  Family
	Buttons Buttons
	Axes    [AxisCount]float32
}

Pad is one connected controller's state.

type Pads

type Pads struct {
	// StickAsDPad makes the left stick press Up/Down/Left/Right too, so
	// menus and grids move with either. On by default.
	StickAsDPad bool
	// NintendoConfirm swaps A and B on Nintendo pads, so South is the A
	// button (which sits east there) and East is B, for players who expect
	// A to confirm as on a Switch. West and North stay by position (Y and
	// X on a Nintendo pad). Off by default: every button goes by position,
	// as on PC, so South (B on a Nintendo pad) confirms. Pads.Label
	// follows it.
	NintendoConfirm bool
	// contains filtered or unexported fields
}

Pads tracks the controllers of one window and turns their state into events, once per frame. Create it with New (platform source) or NewSource; the zero value is not usable. It belongs to the UI goroutine.

func New

func New(host Host) *Pads

New returns Pads reading the platform's controllers, waking host on input. host may be nil when something else draws frames.

func NewSource

func NewSource(src Source, host Host) *Pads

NewSource returns Pads reading src.

func (*Pads) ASCIILabel

func (p *Pads) ASCIILabel(b Button) string

ASCIILabel is Label without symbols (see the package's ASCIILabel).

func (*Pads) Close

func (p *Pads) Close() error

Close stops the source.

func (*Pads) Connected

func (p *Pads) Connected() []Pad

Connected returns the connected pads as of this frame. The slice is reused by the next Frame.

func (*Pads) Err

func (p *Pads) Err() error

Err reports why the source failed to start, if it did. Pads without a working source just report no controllers.

func (*Pads) Events

func (p *Pads) Events() []Event

Events returns this frame's button events, consumed ones included. The slice is reused by the next Frame.

func (*Pads) Family

func (p *Pads) Family() Family

Family is the family of the pad last pressed, or else of the first connected pad: the one to label hints for.

func (*Pads) Frame

func (p *Pads) Frame(gtx layout.Context)

Frame reads the controllers and computes this frame's events, dropping the last frame's. Call it once per frame, before anything reads Events. It schedules the frames that auto-repeat and the web source need.

func (*Pads) Label

func (p *Pads) Label(b Button) string

Label is how the pad to label hints for (Family) prints b, following NintendoConfirm: with it, South is "A" on a Nintendo pad too.

type Source

type Source interface {
	// Start begins reading. The source calls wake, from any goroutine,
	// when a pad connects, disconnects or changes, so the app draws a frame.
	Start(wake func()) error
	// Poll appends every connected pad's current state to dst.
	Poll(dst []Pad) []Pad
	// Close stops reading and releases the devices.
	Close() error
}

Source reads controllers. Platform sources come from New; Fake is one for tests. A Source is used from the UI goroutine except for wake.

Jump to

Keyboard shortcuts

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