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.

Controller.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* global $, APP */
  2. import * as KeyCodes from "../keycode/keycode";
  3. /**
  4. * Extract the keyboard key from the keyboard event.
  5. * @param event {KeyboardEvent} the event.
  6. * @returns {KEYS} the key that is pressed or undefined.
  7. */
  8. function getKey(event) {
  9. return KeyCodes.keyboardEventToKey(event);
  10. }
  11. /**
  12. * Extract the modifiers from the keyboard event.
  13. * @param event {KeyboardEvent} the event.
  14. * @returns {Array} with possible values: "shift", "control", "alt", "command".
  15. */
  16. function getModifiers(event) {
  17. let modifiers = [];
  18. if(event.shiftKey) {
  19. modifiers.push("shift");
  20. }
  21. if(event.ctrlKey) {
  22. modifiers.push("control");
  23. }
  24. if(event.altKey) {
  25. modifiers.push("alt");
  26. }
  27. if(event.metaKey) {
  28. modifiers.push("command");
  29. }
  30. return modifiers;
  31. }
  32. /**
  33. * This class represents the controller party for a remote controller session.
  34. * It listens for mouse and keyboard events and sends them to the receiver
  35. * party of the remote control session.
  36. */
  37. class Controller {
  38. /**
  39. * Creates new instance.
  40. */
  41. constructor() {}
  42. /**
  43. * Starts processing the mouse and keyboard events.
  44. * @param {JQuery.selector} area the selector which will be used for
  45. * attaching the listeners on.
  46. */
  47. start(area) {
  48. this.area = area;
  49. this.area.mousemove(event => {
  50. const position = this.area.position();
  51. this._sendEvent({
  52. type: "mousemove",
  53. x: (event.pageX - position.left)/this.area.width(),
  54. y: (event.pageY - position.top)/this.area.height()
  55. });
  56. });
  57. this.area.mousedown(this._onMouseClickHandler.bind(this, "mousedown"));
  58. this.area.mouseup(this._onMouseClickHandler.bind(this, "mouseup"));
  59. this.area.dblclick(
  60. this._onMouseClickHandler.bind(this, "mousedblclick"));
  61. this.area.contextmenu(() => false);
  62. this.area[0].onmousewheel = event => {
  63. this._sendEvent({
  64. type: "mousescroll",
  65. x: event.deltaX,
  66. y: event.deltaY
  67. });
  68. };
  69. $(window).keydown(this._onKeyPessHandler.bind(this, "keydown"));
  70. $(window).keyup(this._onKeyPessHandler.bind(this, "keyup"));
  71. }
  72. /**
  73. * Stops processing the mouse and keyboard events.
  74. */
  75. stop() {
  76. this.area.off( "mousemove" );
  77. this.area.off( "mousedown" );
  78. this.area.off( "mouseup" );
  79. this.area.off( "contextmenu" );
  80. this.area.off( "dblclick" );
  81. $(window).off( "keydown");
  82. $(window).off( "keyup");
  83. this.area[0].onmousewheel = undefined;
  84. }
  85. /**
  86. * Handler for mouse click events.
  87. * @param {String} type the type of event ("mousedown"/"mouseup")
  88. * @param {Event} event the mouse event.
  89. */
  90. _onMouseClickHandler(type, event) {
  91. this._sendEvent({
  92. type: type,
  93. button: event.which
  94. });
  95. }
  96. /**
  97. * Handler for key press events.
  98. * @param {String} type the type of event ("keydown"/"keyup")
  99. * @param {Event} event the key event.
  100. */
  101. _onKeyPessHandler(type, event) {
  102. this._sendEvent({
  103. type: type,
  104. key: getKey(event),
  105. modifiers: getModifiers(event),
  106. });
  107. }
  108. /**
  109. * Sends remote control event to the controlled participant.
  110. * @param {Object} event the remote control event.
  111. */
  112. _sendRemoteControlEvent(event) {
  113. try{
  114. APP.conference.sendEndpointMessage("",
  115. {type: "remote-control-event", event});
  116. } catch (e) {
  117. // failed to send the event.
  118. }
  119. }
  120. }
  121. export default new Controller();