InputExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. public static bool IsEndedOrCanceled(this TouchPhase phase)
  10. {
  11. return phase == TouchPhase.Canceled || phase == TouchPhase.Ended;
  12. }
  13. public static bool IsActive(this TouchPhase phase)
  14. {
  15. switch (phase)
  16. {
  17. case TouchPhase.Began:
  18. case TouchPhase.Moved:
  19. case TouchPhase.Stationary:
  20. return true;
  21. }
  22. return false;
  23. }
  24. /// <summary>
  25. /// Check if a <see cref="Key"/> enum value represents a modifier key.
  26. /// </summary>
  27. /// <param name="key">The key enum value you want to check.</param>
  28. /// <remarks>
  29. /// Returns true if this key is a modifier key, false otherwise.
  30. /// Modifier keys are any keys you can hold down to modify the output of other keys pressed simultaneously,
  31. /// such as the "shift" or "control" keys.
  32. /// </remarks>
  33. public static bool IsModifierKey(this Key key)
  34. {
  35. switch (key)
  36. {
  37. case Key.LeftAlt:
  38. case Key.RightAlt:
  39. case Key.LeftShift:
  40. case Key.RightShift:
  41. case Key.LeftMeta:
  42. case Key.RightMeta:
  43. case Key.LeftCtrl:
  44. case Key.RightCtrl:
  45. return true;
  46. }
  47. return false;
  48. }
  49. ////REVIEW: Is this a good idea? Ultimately it's up to any one keyboard layout to define this however it wants.
  50. /// <summary>
  51. /// Check if a <see cref="Key"/> enum value represents key generating text input.
  52. /// </summary>
  53. /// <param name="key">The key enum value you want to check.</param>
  54. /// <remarks>
  55. /// Returns true if this key is a key generating non-whitespace character input, false otherwise.
  56. /// </remarks>
  57. public static bool IsTextInputKey(this Key key)
  58. {
  59. switch (key)
  60. {
  61. case Key.LeftShift:
  62. case Key.RightShift:
  63. case Key.LeftAlt:
  64. case Key.RightAlt:
  65. case Key.LeftCtrl:
  66. case Key.RightCtrl:
  67. case Key.LeftMeta:
  68. case Key.RightMeta:
  69. case Key.ContextMenu:
  70. case Key.Escape:
  71. case Key.LeftArrow:
  72. case Key.RightArrow:
  73. case Key.UpArrow:
  74. case Key.DownArrow:
  75. case Key.Backspace:
  76. case Key.PageDown:
  77. case Key.PageUp:
  78. case Key.Home:
  79. case Key.End:
  80. case Key.Insert:
  81. case Key.Delete:
  82. case Key.CapsLock:
  83. case Key.NumLock:
  84. case Key.PrintScreen:
  85. case Key.ScrollLock:
  86. case Key.Pause:
  87. case Key.None:
  88. case Key.Space:
  89. case Key.Enter:
  90. case Key.Tab:
  91. case Key.NumpadEnter:
  92. case Key.F1:
  93. case Key.F2:
  94. case Key.F3:
  95. case Key.F4:
  96. case Key.F5:
  97. case Key.F6:
  98. case Key.F7:
  99. case Key.F8:
  100. case Key.F9:
  101. case Key.F10:
  102. case Key.F11:
  103. case Key.F12:
  104. case Key.OEM1:
  105. case Key.OEM2:
  106. case Key.OEM3:
  107. case Key.OEM4:
  108. case Key.OEM5:
  109. case Key.IMESelected:
  110. return false;
  111. }
  112. return true;
  113. }
  114. }
  115. }