KeyBoard.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.UI;
  9. namespace EZXR.Glass.Inputs
  10. {
  11. public class KeyBoard : MonoBehaviour
  12. {
  13. private static KeyBoard instance;
  14. public static KeyBoard Instance
  15. {
  16. get
  17. {
  18. return instance;
  19. }
  20. }
  21. public KeyBoardHandlerNew keyBoardHandler;
  22. /// <summary>
  23. /// 确认键被按下,用于全局需要通过确认键触发的情况
  24. /// </summary>
  25. public static UnityAction OnEnterClicked
  26. {
  27. get
  28. {
  29. return Instance.keyBoardHandler.OnEnterClicked;
  30. }
  31. set
  32. {
  33. Instance.keyBoardHandler.OnEnterClicked = value;
  34. }
  35. }
  36. public static Action Callback_OnPowerPressed;
  37. public static Action Callback_OnPowerLongPressed;
  38. public static Action Callback_OnVolumeIncreasePressed;
  39. public static Action Callback_OnVolumeDecreasePressed;
  40. public enum KeyEventType
  41. {
  42. Power,
  43. PowerLong,
  44. VolumeIncrease,
  45. VolumeDecrease,
  46. }
  47. public Dictionary<KeyEventType, bool> keyEventHandler = new Dictionary<KeyEventType, bool>();
  48. private void Awake()
  49. {
  50. instance = this;
  51. //keyBoardHandler.OnEnterClicked = OnEnterClicked;
  52. foreach (KeyEventType item in Enum.GetValues(typeof(KeyEventType)))
  53. {
  54. keyEventHandler.Add(item, false);
  55. }
  56. }
  57. // Start is called before the first frame update
  58. void Start()
  59. {
  60. keyBoardHandler.Hide();
  61. }
  62. // Update is called once per frame
  63. void Update()
  64. {
  65. if (EventSystem.current != null && EventSystem.current.currentSelectedGameObject != null && ((HandInputModule.eventData_Left != null && HandInputModule.eventData_Left.pointerPress != null) || (HandInputModule.eventData_Right != null && HandInputModule.eventData_Right.pointerPress != null)))
  66. {
  67. InputField inputField = EventSystem.current.currentSelectedGameObject.GetComponent<InputField>();
  68. TMP_InputField tmpInputField = EventSystem.current.currentSelectedGameObject.GetComponent<TMP_InputField>();
  69. if (inputField != null)
  70. {
  71. keyBoardHandler.Show(inputField);
  72. }
  73. else if (tmpInputField != null)
  74. {
  75. keyBoardHandler.Show(tmpInputField);
  76. }
  77. //else
  78. //{
  79. // keyBoardHandler.Hide();
  80. //}
  81. }
  82. //else
  83. //{
  84. // keyBoard.Hide();
  85. //}
  86. }
  87. //public bool GetKey(KeyEventType keyEventType)
  88. //{
  89. // return keyEventHandler[keyEventType];
  90. //}
  91. //public void GetKeyDown(KeyEventType keyEventType)
  92. //{
  93. //}
  94. //public void GetKeyUp(KeyEventType keyEventType)
  95. //{
  96. //}
  97. }
  98. }