AFPSInputProxy.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #region copyright
  2. // --------------------------------------------------------------
  3. // Copyright (C) Dmitriy Yukhanov - focus [http://codestage.net]
  4. // --------------------------------------------------------------
  5. #endregion
  6. #if UNITY_2019_1_OR_NEWER && ENABLE_INPUT_SYSTEM && AFPS_INPUT_SYSTEM
  7. #define USING_INPUT_SYSTEM
  8. #elif UNITY_2019_1_OR_NEWER && ENABLE_INPUT_SYSTEM
  9. #define USING_INPUT_SYSTEM_NO_INPUT_SYSTEM_PACKAGE
  10. #endif
  11. namespace CodeStage.AdvancedFPSCounter
  12. {
  13. using System;
  14. using UnityEngine;
  15. #if USING_INPUT_SYSTEM
  16. using UnityEngine.InputSystem;
  17. #endif
  18. public static class AFPSInputProxy
  19. {
  20. #if USING_INPUT_SYSTEM
  21. private static Key cachedHotKey;
  22. private static KeyCode lastHotKeyLegacy;
  23. #endif
  24. public static bool GetHotKeyDown(KeyCode key)
  25. {
  26. if (key == KeyCode.None)
  27. return false;
  28. var result = false;
  29. #if USING_INPUT_SYSTEM
  30. if (lastHotKeyLegacy != key)
  31. {
  32. cachedHotKey = ConvertLegacyKeyCode(key);
  33. lastHotKeyLegacy = key;
  34. }
  35. result = Keyboard.current[cachedHotKey].wasPressedThisFrame;
  36. #elif USING_INPUT_SYSTEM_NO_INPUT_SYSTEM_PACKAGE
  37. Debug.LogError("Looks like you have Input System enabled but no Input System package installed!");
  38. #elif ENABLE_LEGACY_INPUT_MANAGER
  39. result = Input.GetKeyDown(key);
  40. #else
  41. result = Input.GetKeyDown(key);
  42. #endif
  43. return result;
  44. }
  45. public static bool GetControlKey()
  46. {
  47. #if USING_INPUT_SYSTEM
  48. return Keyboard.current.leftCtrlKey.isPressed || Keyboard.current.rightCtrlKey.isPressed || Keyboard.current.leftCommandKey.isPressed || Keyboard.current.rightCommandKey.isPressed;
  49. #else
  50. return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftCommand) || Input.GetKey(KeyCode.RightCommand);
  51. #endif
  52. }
  53. public static bool GetAltKey()
  54. {
  55. #if USING_INPUT_SYSTEM
  56. return Keyboard.current.leftAltKey.isPressed || Keyboard.current.rightAltKey.isPressed;
  57. #else
  58. return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
  59. #endif
  60. }
  61. public static bool GetShiftKey()
  62. {
  63. #if USING_INPUT_SYSTEM
  64. return Keyboard.current.leftShiftKey.isPressed || Keyboard.current.rightShiftKey.isPressed;
  65. #else
  66. return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  67. #endif
  68. }
  69. #if USING_INPUT_SYSTEM
  70. private static Key ConvertLegacyKeyCode(KeyCode keyCode)
  71. {
  72. if (!Enum.TryParse(keyCode.ToString(), true, out Key result))
  73. {
  74. Debug.LogError("Couldn't convert legacy input KeyCode " + keyCode + " to the new Input System format!\nPlease report to https://codestage.net/contacts/");
  75. }
  76. return result;
  77. }
  78. #endif
  79. }
  80. }