Listen to events
If you want to do any monitoring or processing on incoming events yourself, subscribe to the InputSystem.onEvent callback.
InputSystem.onEvent +=
(eventPtr, device) =>
{
Debug.Log($"Received event for {device}");
};
An IObservable interface is provided to more conveniently process events.
// Wait for first button press on a gamepad.
InputSystem.onEvent
.ForDevice<Gamepad>()
.Where(e => e.HasButtonPress())
.CallOnce(eventPtr => Debug.Log($"Button {eventPtr.GetFirstButtonPressOrNull()} pressed"));
To enumerate the controls that have value changes in an event, you can use InputControlExtensions.EnumerateChangedControls.
InputSystem.onEvent
.Call(eventPtr =>
{
foreach (var control in eventPtr.EnumerateChangedControls())
Debug.Log($"Control {control} changed value to {control.ReadValueFromEventAsObject(eventPtr)}");
});
This is significantly more efficient than manually iterating over InputDevice.allControls and reading out the value of each control from the event.