Game controller input


The game.input vocabulary provides cross-platform access to game controller devices such as joysticks and gamepads. It also provides an interface for polling raw keyboard and mouse input.

On Linux, GTK3 and GTK4 use events from the focused Factor window for keyboard and mouse input. Mouse polling does not warp the pointer. Controllers are polled through evdev; repeated enumeration reuses device handles until they are disconnected or game input is closed.

With the GTK4 UI backend, keyboard and mouse state comes from events delivered to Factor windows. Losing keyboard focus clears pressed keys and buttons; mouse motion is relative to the preceding pointer event, without warping the pointer.

The game input interface must be initialized before being used:
open-game-input ( -- )

close-game-input ( -- )

with-game-input ( quot -- )


Once the game input interface is open, connected controller devices can be enumerated:
get-controllers ( -- sequence )

find-controller-products ( product-id -- sequence )

find-controller-instance ( product-id instance-id -- controller/f )


These controller objects can be queried of their identity:
product-string ( controller -- string )

product-id ( controller -- id )

instance-id ( controller -- id )


A hook is provided for invoking the system calibration tool:
calibrate-controller ( controller -- )


The current state of a controller, the keyboard, and the mouse can be read:
read-controller ( controller -- controller-state )

read-keyboard ( -- keyboard-state )

read-mouse ( -- mouse-state )

controller-state

keyboard-state

mouse-state


Convenience functions are provided to convert a pair of key or button state sequences into a sequence of pressed/released deltas:
button-delta ( old? new? -- delta )

buttons-delta ( old-buttons new-buttons -- delta )

buttons-delta-as ( old-buttons new-buttons exemplar -- delta )