// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Script used to handle input action events. Invokes Unity events when the configured input action starts or ends. /// [AddComponentMenu("Scripts/MRTK/SDK/InputActionHandler")] public class InputActionHandler : BaseInputHandler, IMixedRealityInputActionHandler { [SerializeField] [Tooltip("Input Action to handle")] private MixedRealityInputAction InputAction = MixedRealityInputAction.None; [SerializeField] [Tooltip("Whether input events should be marked as used after handling so other handlers in the same game object ignore them")] private bool MarkEventsAsUsed = false; /// /// Unity event raised on action start, e.g. button pressed or gesture started. /// Includes the input event that triggered the action. /// public InputActionUnityEvent OnInputActionStarted; /// /// Unity event raised on action end, e.g. button released or gesture completed. /// Includes the input event that triggered the action. /// public InputActionUnityEvent OnInputActionEnded; #region InputSystemGlobalHandlerListener Implementation /// protected override void RegisterHandlers() { CoreServices.InputSystem?.RegisterHandler(this); } /// protected override void UnregisterHandlers() { CoreServices.InputSystem?.UnregisterHandler(this); } #endregion InputSystemGlobalHandlerListener Implementation void IMixedRealityInputActionHandler.OnActionStarted(BaseInputEventData eventData) { if (eventData.MixedRealityInputAction == InputAction && !eventData.used) { OnInputActionStarted.Invoke(eventData); if (MarkEventsAsUsed) { eventData.Use(); } } } void IMixedRealityInputActionHandler.OnActionEnded(BaseInputEventData eventData) { if (eventData.MixedRealityInputAction == InputAction && !eventData.used) { OnInputActionEnded.Invoke(eventData); if (MarkEventsAsUsed) { eventData.Use(); } } } } }