KeyBoardHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using EZXR.Glass.Core;
  2. using EZXR.Glass.UI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.UI;
  10. namespace EZXR.Glass.Inputs
  11. {
  12. //[ExecuteInEditMode]
  13. public class KeyBoardHandler : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// 确认键被按下,用于全局需要通过确认键触发的情况
  17. /// </summary>
  18. public UnityAction OnEnterClicked;
  19. private TMP_InputField curTMPInputField;
  20. private InputField curInputField;
  21. public TextMeshPro m_TextMeshPro;
  22. public SpatialButton[] letters;
  23. public SpatialButton_Thick symbol;
  24. /// <summary>
  25. /// 左侧小键盘
  26. /// </summary>
  27. public SpatialButton[] symbols;
  28. /// <summary>
  29. /// 与数字键盘一一对应的符号,用于切换符号和数字
  30. /// </summary>
  31. public string[] symbols4Change;
  32. public string[] num4Change;
  33. public SpatialButton_Thick capslock;
  34. /// <summary>
  35. /// 0是非按下,1是按下
  36. /// </summary>
  37. public Texture2D[] capslockStatus;
  38. private void Awake()
  39. {
  40. SpatialButton_Thick[] buttons = transform.GetComponentsInChildren<SpatialButton_Thick>();
  41. foreach (SpatialButton_Thick button in buttons)
  42. {
  43. button.AddEvent(SpatialButton.SpatialButtonEventType.OnPointerClicked, () => OnKeyClicked(button));
  44. }
  45. }
  46. private void KeyboardRecenter(bool enabled = false)
  47. {
  48. transform.Recenter(new Vector3(0, -0.3f, 0.5f), false);
  49. }
  50. void OnEnable()
  51. {
  52. //SystemManager.OnPowerPressed += KeyboardRecenter;
  53. //#if UNITY_EDITOR
  54. // foreach (SpatialButton button in letters)
  55. // {
  56. // button.Text = button.name;
  57. // }
  58. // foreach (SpatialButton button in symbols)
  59. // {
  60. // button.Text = button.name;
  61. // }
  62. //#endif
  63. }
  64. private void OnDisable()
  65. {
  66. //WristPanel.Left.RemoveListener(KeyboardRecenter);
  67. //SystemManager.OnPowerPressed -= KeyboardRecenter;
  68. }
  69. private void Start()
  70. {
  71. if (Application.isPlaying)
  72. {
  73. ChangeLettersToLower();
  74. ChangeSymbols("#+&");
  75. }
  76. }
  77. // Update is called once per frame
  78. void Update()
  79. {
  80. //if (Input.GetKeyDown(KeyCode.T))
  81. //{
  82. // KeyboardRecenter();
  83. //}
  84. }
  85. public void Show(InputField inputField)
  86. {
  87. KeyboardRecenter();
  88. curInputField = inputField;
  89. m_TextMeshPro.text = inputField.text;
  90. gameObject.SetActive(true);
  91. }
  92. public void Show(TMP_InputField inputField)
  93. {
  94. KeyboardRecenter();
  95. curTMPInputField = inputField;
  96. m_TextMeshPro.text = inputField.text;
  97. gameObject.SetActive(true);
  98. }
  99. public void Hide()
  100. {
  101. curInputField = null;
  102. curTMPInputField = null;
  103. gameObject.SetActive(false);
  104. }
  105. void ExchangeLetters()
  106. {
  107. if (char.IsLower(letters[0].Text[0]))
  108. {
  109. ChangeLettersToUpper();
  110. }
  111. else if (char.IsUpper(letters[0].Text[0]))
  112. {
  113. ChangeLettersToLower();
  114. }
  115. }
  116. void ChangeLettersToUpper()
  117. {
  118. foreach (SpatialButton button in letters)
  119. {
  120. button.Text = button.Text.ToUpper();
  121. }
  122. //CapsLock非按下状态
  123. capslock.Icon = capslockStatus[1];
  124. }
  125. void ChangeLettersToLower()
  126. {
  127. foreach (SpatialButton button in letters)
  128. {
  129. button.Text = button.Text.ToLower();
  130. }
  131. //CapsLock非按下状态
  132. capslock.Icon = capslockStatus[0];
  133. }
  134. void ChangeSymbols(string target = "")
  135. {
  136. if (symbol.Text == "123" || target == "#+&")
  137. {
  138. symbol.Text = "#+&";
  139. for (int i = 0; i < symbols.Length; i++)
  140. {
  141. symbols[i].Text = num4Change[i];
  142. }
  143. }
  144. else if (symbol.Text == "#+&" || target == "123")
  145. {
  146. symbol.Text = "123";
  147. for (int i = 0; i < symbols.Length; i++)
  148. {
  149. symbols[i].Text = symbols4Change[i];
  150. }
  151. }
  152. }
  153. public void OnKeyHoverEnter(string key)
  154. {
  155. Debug.Log("OnKeyHoverEnter: " + key);
  156. }
  157. public void OnStartPress(string key)
  158. {
  159. Debug.Log("OnStartPress: " + key);
  160. }
  161. public void OnPressing(string key)
  162. {
  163. Debug.Log("OnPressing: " + key);
  164. }
  165. //OnKeyDown
  166. public void OnKeyDown(string key)
  167. {
  168. Debug.Log("OnKeyDown: " + key);
  169. }
  170. //OnKeyUp
  171. public void OnKeyUp(string key)
  172. {
  173. Debug.Log("OnKeyUp: " + key);
  174. }
  175. public void OnEndPress(string key)
  176. {
  177. Debug.Log("OnEndPress: " + key);
  178. }
  179. public void OnKeyClicked(SpatialButton sender)
  180. {
  181. switch (sender.Text)
  182. {
  183. case "Del":
  184. if (m_TextMeshPro.text.Length > 0)
  185. {
  186. m_TextMeshPro.text = m_TextMeshPro.text.Substring(0, m_TextMeshPro.text.Length - 1);
  187. }
  188. break;
  189. case "#+&":
  190. ChangeSymbols("123");
  191. break;
  192. case "123":
  193. ChangeSymbols("#+&");
  194. break;
  195. case "CapsLock":
  196. ExchangeLetters();
  197. break;
  198. case "Language":
  199. break;
  200. case "Return":
  201. //m_TextMeshPro.text += "\r\n";
  202. if (OnEnterClicked != null)
  203. {
  204. OnEnterClicked.Invoke();
  205. }
  206. break;
  207. case "Keyboard":
  208. Hide();
  209. break;
  210. default:
  211. m_TextMeshPro.text += sender.Text;
  212. break;
  213. }
  214. if (curInputField != null)
  215. {
  216. curInputField.text = m_TextMeshPro.text;
  217. }
  218. if (curTMPInputField != null)
  219. {
  220. curTMPInputField.text = m_TextMeshPro.text;
  221. }
  222. }
  223. public void OnKeyHoverExit(string key)
  224. {
  225. Debug.Log("OnKeyHoverExit: " + key);
  226. }
  227. }
  228. }