KeyEvents.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  2. #define ZF_OSX
  3. #endif
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace ZenFulcrum.EmbeddedBrowser {
  7. /// <summary>
  8. /// Helper class for IBrowserUI implementations for getting/generating keyboard events for sending to an IBrowserUI.
  9. /// </summary>
  10. public class KeyEvents {
  11. /** Fills up with key events as they happen. */
  12. protected List<Event> keyEvents = new List<Event>();
  13. /** Swaps with keyEvents on InputUpdate and is returned in the main getter. */
  14. protected List<Event> keyEventsLast = new List<Event>();
  15. /// <summary>
  16. /// After calling InputUpdate, contains the key events to send to the browser.
  17. /// </summary>
  18. public List<Event> Events {
  19. get { return keyEventsLast; }
  20. }
  21. /** List of keys Unity won't give us events for. So we have to poll. */
  22. static readonly KeyCode[] keysToCheck = {
  23. #if ZF_OSX
  24. //On windows you get GUI events for ctrl, super, alt. On mac...you don't!
  25. KeyCode.LeftCommand,
  26. KeyCode.RightCommand,
  27. KeyCode.LeftControl,
  28. KeyCode.RightControl,
  29. KeyCode.LeftAlt,
  30. KeyCode.RightAlt,
  31. //KeyCode.CapsLock, unity refuses to inform us of this, so there's not much we can do
  32. #endif
  33. //Unity consistently doesn't send events for shift across all platforms.
  34. KeyCode.LeftShift,
  35. KeyCode.RightShift,
  36. };
  37. /// <summary>
  38. /// Call once per frame before grabbing
  39. /// </summary>
  40. public void InputUpdate() {
  41. //Note: keyEvents gets filled in OnGUI as things happen. InputUpdate get called just before it's read.
  42. //To get the right events to the right place at the right time, swap the "double buffer" of key events.
  43. var tmp = keyEvents;
  44. keyEvents = keyEventsLast;
  45. keyEventsLast = tmp;
  46. keyEvents.Clear();
  47. //Unity doesn't include events for some keys, so fake it by checking each frame.
  48. for (int i = 0; i < keysToCheck.Length; i++) {
  49. if (Input.GetKeyDown(keysToCheck[i])) {
  50. //Prepend down, postpend up. We don't know which happened first, but pressing
  51. //modifiers usually precedes other key presses and releasing tends to follow.
  52. keyEventsLast.Insert(0, new Event() { type = EventType.KeyDown, keyCode = keysToCheck[i] });
  53. } else if (Input.GetKeyUp(keysToCheck[i])) {
  54. keyEventsLast.Add(new Event() { type = EventType.KeyUp, keyCode = keysToCheck[i] });
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// Call this with any GUI events you get from Unity that you want to have passed to the browser.
  60. /// </summary>
  61. /// <param name="ev"></param>
  62. public void Feed(Event ev) {
  63. if (ev.type != EventType.KeyDown && ev.type != EventType.KeyUp) return;
  64. // if (ev.character != 0) Debug.Log("ev >>> " + ev.character);
  65. // else if (ev.type == EventType.KeyUp) Debug.Log("ev ^^^ " + ev.keyCode);
  66. // else if (ev.type == EventType.KeyDown) Debug.Log("ev vvv " + ev.keyCode);
  67. keyEvents.Add(new Event(ev));
  68. }
  69. /// <summary>
  70. /// Injects a key press. Call Release laster to let go.
  71. /// If the key you are pressing represents a character this may not type that character. Use Type() instead.
  72. /// </summary>
  73. /// <param name="key"></param>
  74. public void Press(KeyCode key) {
  75. keyEvents.Add(new Event {
  76. type = EventType.KeyDown, keyCode = key
  77. });
  78. }
  79. public void Release(KeyCode key) {
  80. keyEvents.Add(new Event {
  81. type = EventType.KeyUp, keyCode = key
  82. });
  83. }
  84. /// <summary>
  85. /// Types the given text in. THis does not simulate pressing each key and releasing it.
  86. /// </summary>
  87. /// <param name="text"></param>
  88. public void Type(string text) {
  89. for (int i = 0; i < text.Length; i++) {
  90. //fixme: multibyte chars >16 bits
  91. keyEvents.Add(new Event {
  92. type = EventType.KeyDown, character = text[i],
  93. });
  94. }
  95. }
  96. }
  97. }