Documentation ¶
Overview ¶
Package joystick provides utilities for querying and reacting to joystick or gamepad inputs.
Index ¶
Constants ¶
const ( Change = "JoystickChange" ButtonDown = "ButtonDown" ButtonUp = "ButtonUp" RtTriggerChange = "RtTriggerChange" LtTriggerChange = "LtTriggerChange" RtStickChange = "RtStickChange" LtStickChange = "LtStickChange" Disconnected = "JoystickDisconnected" )
Events
Variables ¶
This section is empty.
Functions ¶
func WaitForJoysticks ¶
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.
Types ¶
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 ¶
Close closes any operating system resources backing this joystick's signals
func (*Joystick) GetState ¶
GetState returns the current button, trigger, and analog stick state of the joystick
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.
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 }
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