using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class KeyboardShortcutManager : MonoBehaviour { public void AddKeyData(GameObject target, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp) { this.m_CommandDataList.Add(new KeyboardShortcutManager.CommandData(target, keyCode, onKeyDown, onKeyStay, onKeyUp)); } private void Update() { this.m_NowInputKey = KeyCode.None; if (Input.anyKey) { IEnumerator enumerator = Enum.GetValues(typeof(KeyCode)).GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; KeyCode keyCode = (KeyCode)obj; if (Input.GetKey(keyCode)) { this.m_NowInputKey = keyCode; break; } } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } this.UpdateKey(); } private void UpdateKey() { if (this.m_CommandDataList == null) { return; } foreach (KeyboardShortcutManager.CommandData commandData in this.m_CommandDataList) { if (!(commandData.targetObject == null)) { bool flag = this.m_BeforeInputKey == commandData.keyCode; bool flag2 = this.m_NowInputKey == commandData.keyCode; if (!flag && flag2) { commandData.onKeyDown(); } else if (flag && flag2) { commandData.onKeyStay(); } else if (flag && !flag2) { commandData.onKeyUp(); } } } this.m_BeforeInputKey = this.m_NowInputKey; } private void OnApplicationQuit() { this.m_IsApplicationQuit = true; } private void OnDestroy() { if (this.m_IsApplicationQuit) { return; } this.m_CommandDataList.Clear(); this.m_CommandDataList = null; } private HashSet m_CommandDataList = new HashSet(); private KeyCode m_BeforeInputKey; private KeyCode m_NowInputKey; private bool m_IsApplicationQuit; public class CommandData { public CommandData(GameObject targetObject, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp) { this.targetObject = targetObject; this.keyCode = keyCode; this.onKeyDown = onKeyDown; this.onKeyStay = onKeyStay; this.onKeyUp = onKeyUp; } public GameObject targetObject { get; private set; } public KeyCode keyCode { get; set; } public Action onKeyDown { get; set; } public Action onKeyStay { get; set; } public Action onKeyUp { get; set; } } }