KeyboardShortcutManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class KeyboardShortcutManager : MonoBehaviour
  6. {
  7. public void AddKeyData(GameObject target, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp)
  8. {
  9. this.m_CommandDataList.Add(new KeyboardShortcutManager.CommandData(target, keyCode, onKeyDown, onKeyStay, onKeyUp));
  10. }
  11. private void Update()
  12. {
  13. this.m_NowInputKey = KeyCode.None;
  14. if (Input.anyKey)
  15. {
  16. IEnumerator enumerator = Enum.GetValues(typeof(KeyCode)).GetEnumerator();
  17. try
  18. {
  19. while (enumerator.MoveNext())
  20. {
  21. object obj = enumerator.Current;
  22. KeyCode keyCode = (KeyCode)obj;
  23. if (Input.GetKey(keyCode))
  24. {
  25. this.m_NowInputKey = keyCode;
  26. break;
  27. }
  28. }
  29. }
  30. finally
  31. {
  32. IDisposable disposable;
  33. if ((disposable = (enumerator as IDisposable)) != null)
  34. {
  35. disposable.Dispose();
  36. }
  37. }
  38. }
  39. this.UpdateKey();
  40. }
  41. private void UpdateKey()
  42. {
  43. if (this.m_CommandDataList == null)
  44. {
  45. return;
  46. }
  47. foreach (KeyboardShortcutManager.CommandData commandData in this.m_CommandDataList)
  48. {
  49. if (!(commandData.targetObject == null))
  50. {
  51. bool flag = this.m_BeforeInputKey == commandData.keyCode;
  52. bool flag2 = this.m_NowInputKey == commandData.keyCode;
  53. if (!flag && flag2)
  54. {
  55. commandData.onKeyDown();
  56. }
  57. else if (flag && flag2)
  58. {
  59. commandData.onKeyStay();
  60. }
  61. else if (flag && !flag2)
  62. {
  63. commandData.onKeyUp();
  64. }
  65. }
  66. }
  67. this.m_BeforeInputKey = this.m_NowInputKey;
  68. }
  69. private void OnApplicationQuit()
  70. {
  71. this.m_IsApplicationQuit = true;
  72. }
  73. private void OnDestroy()
  74. {
  75. if (this.m_IsApplicationQuit)
  76. {
  77. return;
  78. }
  79. this.m_CommandDataList.Clear();
  80. this.m_CommandDataList = null;
  81. }
  82. private HashSet<KeyboardShortcutManager.CommandData> m_CommandDataList = new HashSet<KeyboardShortcutManager.CommandData>();
  83. private KeyCode m_BeforeInputKey;
  84. private KeyCode m_NowInputKey;
  85. private bool m_IsApplicationQuit;
  86. public class CommandData
  87. {
  88. public CommandData(GameObject targetObject, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp)
  89. {
  90. this.targetObject = targetObject;
  91. this.keyCode = keyCode;
  92. this.onKeyDown = onKeyDown;
  93. this.onKeyStay = onKeyStay;
  94. this.onKeyUp = onKeyUp;
  95. }
  96. public GameObject targetObject { get; private set; }
  97. public KeyCode keyCode { get; set; }
  98. public Action onKeyDown { get; set; }
  99. public Action onKeyStay { get; set; }
  100. public Action onKeyUp { get; set; }
  101. }
  102. }