暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputExtensions.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ////REVIEW: move everything from InputControlExtensions here?
  2. namespace UnityEngine.InputSystem
  3. {
  4. /// <summary>
  5. /// Various useful extension methods.
  6. /// </summary>
  7. public static class InputExtensions
  8. {
  9. /// <summary>
  10. /// Return true if the given phase is <see cref="InputActionPhase.Started"/> or <see cref="InputActionPhase.Performed"/>.
  11. /// </summary>
  12. /// <param name="phase">An action phase.</param>
  13. /// <returns>True if the phase is started or performed.</returns>
  14. /// <seealso cref="InputAction.phase"/>
  15. public static bool IsInProgress(this InputActionPhase phase)
  16. {
  17. return phase == InputActionPhase.Started || phase == InputActionPhase.Performed;
  18. }
  19. /// <summary>
  20. /// Return true if the given phase is <see cref="TouchPhase.Canceled"/> or <see cref="TouchPhase.Ended"/>, i.e.
  21. /// if a touch with that phase would no longer be ongoing.
  22. /// </summary>
  23. /// <param name="phase">A touch phase.</param>
  24. /// <returns>True if the phase indicates a touch that has ended.</returns>
  25. /// <seealso cref="Controls.TouchControl.phase"/>
  26. public static bool IsEndedOrCanceled(this TouchPhase phase)
  27. {
  28. return phase == TouchPhase.Canceled || phase == TouchPhase.Ended;
  29. }
  30. /// <summary>
  31. /// Return true if the given phase is <see cref="TouchPhase.Began"/>, <see cref="UnityEngine.TouchPhase.Moved"/>, or
  32. /// <see cref="TouchPhase.Stationary"/>, i.e. if a touch with that phase would indicate an ongoing touch.
  33. /// </summary>
  34. /// <param name="phase">A touch phase.</param>
  35. /// <returns>True if the phase indicates a touch that is ongoing.</returns>
  36. /// <seealso cref="Controls.TouchControl.phase"/>
  37. public static bool IsActive(this TouchPhase phase)
  38. {
  39. switch (phase)
  40. {
  41. case TouchPhase.Began:
  42. case TouchPhase.Moved:
  43. case TouchPhase.Stationary:
  44. return true;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// Check if a <see cref="Key"/> enum value represents a modifier key.
  50. /// </summary>
  51. /// <param name="key">The key enum value you want to check.</param>
  52. /// <returns><c>true</c> if <paramref name="key"/> represents a modifier key, else <c>false</c>.</returns>
  53. /// <remarks>
  54. /// Modifier keys are any keys you can hold down to modify the output of other keys pressed simultaneously,
  55. /// such as the "shift" or "control" keys.
  56. /// </remarks>
  57. public static bool IsModifierKey(this Key key)
  58. {
  59. switch (key)
  60. {
  61. case Key.LeftAlt:
  62. case Key.RightAlt:
  63. case Key.LeftShift:
  64. case Key.RightShift:
  65. case Key.LeftMeta:
  66. case Key.RightMeta:
  67. case Key.LeftCtrl:
  68. case Key.RightCtrl:
  69. return true;
  70. }
  71. return false;
  72. }
  73. ////REVIEW: Is this a good idea? Ultimately it's up to any one keyboard layout to define this however it wants.
  74. /// <summary>
  75. /// Check if a <see cref="Key"/> enum value represents key generating text input.
  76. /// </summary>
  77. /// <param name="key">The key enum value you want to check.</param>
  78. /// <returns><c>true</c> if <paramref name="key"/> represents a key generating non-whitespace text input, else <c>false</c>.</returns>
  79. public static bool IsTextInputKey(this Key key)
  80. {
  81. switch (key)
  82. {
  83. case Key.LeftShift:
  84. case Key.RightShift:
  85. case Key.LeftAlt:
  86. case Key.RightAlt:
  87. case Key.LeftCtrl:
  88. case Key.RightCtrl:
  89. case Key.LeftMeta:
  90. case Key.RightMeta:
  91. case Key.ContextMenu:
  92. case Key.Escape:
  93. case Key.LeftArrow:
  94. case Key.RightArrow:
  95. case Key.UpArrow:
  96. case Key.DownArrow:
  97. case Key.Backspace:
  98. case Key.PageDown:
  99. case Key.PageUp:
  100. case Key.Home:
  101. case Key.End:
  102. case Key.Insert:
  103. case Key.Delete:
  104. case Key.CapsLock:
  105. case Key.NumLock:
  106. case Key.PrintScreen:
  107. case Key.ScrollLock:
  108. case Key.Pause:
  109. case Key.None:
  110. case Key.Space:
  111. case Key.Enter:
  112. case Key.Tab:
  113. case Key.NumpadEnter:
  114. case Key.F1:
  115. case Key.F2:
  116. case Key.F3:
  117. case Key.F4:
  118. case Key.F5:
  119. case Key.F6:
  120. case Key.F7:
  121. case Key.F8:
  122. case Key.F9:
  123. case Key.F10:
  124. case Key.F11:
  125. case Key.F12:
  126. case Key.OEM1:
  127. case Key.OEM2:
  128. case Key.OEM3:
  129. case Key.OEM4:
  130. case Key.OEM5:
  131. case Key.IMESelected:
  132. return false;
  133. }
  134. return true;
  135. }
  136. }
  137. }