Version: v4.0.0-alpha.2 Latest Latest

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

Go to latest
Published: Apr 23, 2022 License: Apache-2.0 Imports: 15 Imported by: 0



Package joystick provides utilities for querying and reacting to joystick or gamepad inputs.



This section is empty.


View Source
var (
	Change          = event.RegisterEvent[*State]()
	ButtonDown      = event.RegisterEvent[*State]()
	ButtonUp        = event.RegisterEvent[*State]()
	RtTriggerChange = event.RegisterEvent[*State]()
	LtTriggerChange = event.RegisterEvent[*State]()
	RtStickChange   = event.RegisterEvent[*State]()
	LtStickChange   = event.RegisterEvent[*State]()
	// Disconnected includes the ID of the joystick that disconnected.
	Disconnected = event.RegisterEvent[uint32]()

Events. All events but Disconnected include a *State payload.


func Down

func Down(s string) event.EventID[*State]

func Init

func Init() error

Init calls any os functions necessary to detect joysticks

func Up

func Up(s string) event.EventID[*State]

func WaitForJoysticks

func WaitForJoysticks(pollRate time.Duration) (joyCh <-chan *Joystick, cancel func())

WaitForJoysticks will regularly call GetJoysticks so to send signals on the output channel when new joysticks are connected to the system. Call `cancel' to close the channel and stop polling.


type Input

type Input string
const (
	InputA             Input = "A"
	InputB             Input = "B"
	InputX             Input = "X"
	InputY             Input = "Y"
	InputUp            Input = "Up"
	InputDown          Input = "Down"
	InputLeft          Input = "Left"
	InputRight         Input = "Right"
	InputBack          Input = "Back"
	InputStart         Input = "Start"
	InputLeftShoulder  Input = "LeftShoulder"
	InputRightShoulder Input = "RightShoulder"
	InputLeftStick     Input = "LeftStick"
	InputRightStick    Input = "RightStick"

Button or input names

type Joystick

type Joystick struct {
	Handler  Triggerer
	PollRate time.Duration
	// contains filtered or unexported fields

A Joystick represents a (usually) physical controller connected to the machine. It can be listened to to obtain button / trigger states of the controller and propagate changes through an event handler.

func GetJoysticks

func GetJoysticks() []*Joystick

GetJoysticks returns all known active joysticks.

func (*Joystick) Close

func (j *Joystick) Close() error

Close closes any operating system resources backing this joystick's signals

func (*Joystick) GetState

func (j *Joystick) GetState() (*State, error)

GetState returns the current button, trigger, and analog stick state of the joystick

func (*Joystick) ID

func (j *Joystick) ID() uint32

ID returns which player this joystick is associated with

func (*Joystick) Listen

func (j *Joystick) Listen(opts *ListenOptions) (cancel func())

Listen causes the joystick to send its inputs to its Handler, by regularly querying GetState. The type of events returned can be specified by options. If the options are nil, only JoystickChange events will be sent.

func (*Joystick) Prepare

func (j *Joystick) Prepare() error

Prepare allocates any operating system resources needed to read signals for the joystick.

func (*Joystick) Vibrate

func (j *Joystick) Vibrate(left, right uint16) error

Vibrate triggers vibration on a joystick (if it is supported).

type ListenOptions

type ListenOptions struct {
	// Each boolean dictates given event type(s) will be sent during Listen
	// "JoystickChange": *State
	JoystickChanges bool
	// Whenever any button is changed:
	// "ButtonDown": *State
	// "ButtonUp": *State
	GenericButtonPresses bool
	// "($buttonName)Up/Down": *State
	// e.g.
	// "AButtonUp": *State
	// "XButtonDown:" *State
	ButtonPresses bool
	// "RtTriggerChange": *State
	// "LtTriggerChange": *State
	TriggerChanges bool
	// "RtStickChange": *State
	// "LtStickChange": *State
	StickChanges bool
	// StickDeadzones enable preventing movement near the center of
	// the analog control being sent to a logic handler. A
	// StickDeadzone value will be treated as an absolute threshold.
	StickDeadzoneLX int16
	StickDeadzoneLY int16
	StickDeadzoneRX int16
	StickDeadzoneRY int16

ListenOptions can be passed into a joystick's Listen method to change what events will be propagated out.

type State

type State struct {
	// Frame advances every time the State changes,
	// Making it easier to tell if the state has not changed
	// since the last poll.
	Frame uint32
	// ID is the joystick ID this state is associated with
	ID uint32
	// There isn't a canonical list of buttons (yet), because
	// this can be so dependent on controller type. To get a list of expected
	// buttons for a joystick, this map can be checked after a single GetState().
	// The package will ensure that the keyset of this map will never change for
	// a given joystick variable. True = Pressed/Down. False = Released/Up.
	// Todo: ensure the above guarantee
	Buttons  map[string]bool
	TriggerL uint8
	TriggerR uint8
	StickLX  int16
	StickLY  int16
	StickRX  int16
	StickRY  int16

State represents a snapshot of a joystick's inputs

type Triggerer

type Triggerer interface {
	Trigger(eventID event.UnsafeEventID, data interface{}) <-chan struct{}

A Triggerer can either be an event bus or event CID, allowing joystick triggers to be listened to globally or sent to particular entities.

Jump to

Keyboard shortcuts

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