docs.unity3d.com
    Show / Hide Table of Contents

    Struct InputEventListener

    Wraps around mechanisms for listening in on the InputEvent stream made available through onEvent.

    Inherited Members
    ValueType.Equals(Object)
    ValueType.GetHashCode()
    ValueType.ToString()
    Namespace: UnityEngine.InputSystem.LowLevel
    Syntax
    public struct InputEventListener : IObservable<InputEventPtr>
    Remarks

    This struct can be used to add (Addition(InputEventListener, Action<InputEventPtr, InputDevice>)) or remove (Subtraction(InputEventListener, Action<InputEventPtr, InputDevice>)) callbacks directly to/from the event pipeline.

    Alternatively, it can be used as an IObservable to Subscribe(IObserver<InputEventPtr>) observers to the event stream. See Observable for extension methods to set up various observer mechanisms.

    InputSystem.onEvent
        .ForDevice(Mouse.current)
        .Call(evt =>
            {
                foreach (var control in evt.EnumerateChangedControls())
                    Debug.Log($"Control {control} on mouse has changed value");
            });

    Methods

    Subscribe(IObserver<InputEventPtr>)

    Subscribe an observer to the event pump.

    Declaration
    public IDisposable Subscribe(IObserver<InputEventPtr> observer)
    Parameters
    Type Name Description
    IObserver<InputEventPtr> observer

    Observer to be notified for each event.

    Returns
    Type Description
    IDisposable

    A handle to dispose of the subscription.

    Implements
    IObservable<T>.Subscribe(IObserver<T>)
    Remarks

    The easiest way to subscribe is via the extension methods in Observable.

    // Subscribe.
    var subscription = InputSystem.onEvent.Call(e => Debug.Log("Event"));
    
    // Unsubscribe.
    subscription.Dispose();

    Operators

    Addition(InputEventListener, Action<InputEventPtr, InputDevice>)

    Add a delegate to be called for each InputEvent that is processed by the Input System.

    Declaration
    public static InputEventListener operator +(InputEventListener _, Action<InputEventPtr, InputDevice> callback)
    Parameters
    Type Name Description
    InputEventListener _
    Action<InputEventPtr, InputDevice> callback

    A callback to call for each event.

    Returns
    Type Description
    InputEventListener

    The same listener instance.

    Remarks
    InputSystem.onEvent +=
        (eventPtr, device) =>
        {
            Debug.Log($"Event for {device}");
        };
    Exceptions
    Type Condition
    ArgumentNullException

    callback is null.

    See Also
    onEvent

    Subtraction(InputEventListener, Action<InputEventPtr, InputDevice>)

    Remove a delegate from InputEvent.

    Declaration
    public static InputEventListener operator -(InputEventListener _, Action<InputEventPtr, InputDevice> callback)
    Parameters
    Type Name Description
    InputEventListener _
    Action<InputEventPtr, InputDevice> callback

    A callback that was previously installed on onEvent.

    Returns
    Type Description
    InputEventListener

    The same listener instance.

    Remarks
    InputSystem.onEvent -= myDelegate;
    Exceptions
    Type Condition
    ArgumentNullException

    callback is null.

    See Also
    onEvent

    Extension Methods

    Observable.Where<TValue>(IObservable<TValue>, Func<TValue, Boolean>)
    Observable.Select<TSource, TResult>(IObservable<TSource>, Func<TSource, TResult>)
    Observable.SelectMany<TSource, TResult>(IObservable<TSource>, Func<TSource, IEnumerable<TResult>>)
    Observable.Take<TValue>(IObservable<TValue>, Int32)
    Observable.ForDevice(IObservable<InputEventPtr>, InputDevice)
    Observable.ForDevice<TDevice>(IObservable<InputEventPtr>)
    Observable.CallOnce<TValue>(IObservable<TValue>, Action<TValue>)
    Observable.Call<TValue>(IObservable<TValue>, Action<TValue>)

    See Also

    onEvent
    In This Article
    • Methods
      • Subscribe(IObserver<InputEventPtr>)
    • Operators
      • Addition(InputEventListener, Action<InputEventPtr, InputDevice>)
      • Subtraction(InputEventListener, Action<InputEventPtr, InputDevice>)
    • Extension Methods
    • See Also
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023