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.

keyboardshortcut.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* global APP, $ */
  2. import AnalyticsAdapter from '../statistics/AnalyticsAdapter';
  3. //maps keycode to character, id of popover for given function and function
  4. var shortcuts = {};
  5. function initShortcutHandlers() {
  6. shortcuts = {
  7. "ESCAPE": {
  8. character: "Esc",
  9. function: function() {
  10. APP.UI.showKeyboardShortcutsPanel(false);
  11. }
  12. },
  13. "C": {
  14. character: "C",
  15. id: "toggleChatPopover",
  16. function: function() {
  17. AnalyticsAdapter.sendEvent('shortcut.chat.toggled');
  18. APP.UI.toggleChat();
  19. }
  20. },
  21. "D": {
  22. character: "D",
  23. id: "toggleDesktopSharingPopover",
  24. function: function () {
  25. AnalyticsAdapter.sendEvent('shortcut.screen.toggled');
  26. APP.conference.toggleScreenSharing();
  27. }
  28. },
  29. "F": {
  30. character: "F",
  31. id: "filmstripPopover",
  32. function: function() {
  33. AnalyticsAdapter.sendEvent('shortcut.film.toggled');
  34. APP.UI.toggleFilmStrip();
  35. }
  36. },
  37. "M": {
  38. character: "M",
  39. id: "mutePopover",
  40. function: function() {
  41. AnalyticsAdapter.sendEvent('shortcut.audiomute.toggled');
  42. APP.conference.toggleAudioMuted();
  43. }
  44. },
  45. "R": {
  46. character: "R",
  47. function: function() {
  48. AnalyticsAdapter.sendEvent('shortcut.raisedhand.toggled');
  49. APP.conference.maybeToggleRaisedHand();
  50. }
  51. },
  52. "T": {
  53. character: "T",
  54. function: function() {
  55. AnalyticsAdapter.sendEvent('shortcut.talk.clicked');
  56. APP.conference.muteAudio(true);
  57. }
  58. },
  59. "V": {
  60. character: "V",
  61. id: "toggleVideoPopover",
  62. function: function() {
  63. AnalyticsAdapter.sendEvent('shortcut.videomute.toggled');
  64. APP.conference.toggleVideoMuted();
  65. }
  66. },
  67. "?": {
  68. character: "?",
  69. function: function(e) {
  70. AnalyticsAdapter.sendEvent('shortcut.shortcut.help');
  71. APP.UI.toggleKeyboardShortcutsPanel();
  72. }
  73. }
  74. };
  75. }
  76. var KeyboardShortcut = {
  77. init: function () {
  78. initShortcutHandlers();
  79. var self = this;
  80. window.onkeyup = function(e) {
  81. var key = self.getKeyboardKey(e).toUpperCase();
  82. var num = parseInt(key, 10);
  83. if(!($(":focus").is("input[type=text]") ||
  84. $(":focus").is("input[type=password]") ||
  85. $(":focus").is("textarea"))) {
  86. if (shortcuts.hasOwnProperty(key)) {
  87. shortcuts[key].function(e);
  88. }
  89. else if (!isNaN(num) && num >= 0 && num <= 9) {
  90. APP.UI.clickOnVideo(num + 1);
  91. }
  92. //esc while the smileys are visible hides them
  93. } else if (key === "ESCAPE" &&
  94. $('#smileysContainer').is(':visible')) {
  95. APP.UI.toggleSmileys();
  96. }
  97. };
  98. window.onkeydown = function(e) {
  99. if(!($(":focus").is("input[type=text]") ||
  100. $(":focus").is("input[type=password]") ||
  101. $(":focus").is("textarea"))) {
  102. var key = self.getKeyboardKey(e).toUpperCase();
  103. if(key === "T") {
  104. if(APP.conference.isLocalAudioMuted())
  105. APP.conference.muteAudio(false);
  106. }
  107. }
  108. };
  109. $('body').popover({ selector: '[data-toggle=popover]',
  110. trigger: 'click hover',
  111. content: function() {
  112. return this.getAttribute("content") +
  113. self.getShortcut(this.getAttribute("shortcut"));
  114. }
  115. });
  116. },
  117. /**
  118. *
  119. * @param id indicates the popover associated with the shortcut
  120. * @returns {string} the keyboard shortcut used for the id given
  121. */
  122. getShortcut: function (id) {
  123. for (var key in shortcuts) {
  124. if (shortcuts.hasOwnProperty(key)) {
  125. if (shortcuts[key].id === id) {
  126. return " (" + shortcuts[key].character + ")";
  127. }
  128. }
  129. }
  130. return "";
  131. },
  132. /**
  133. * @param e a KeyboardEvent
  134. * @returns {string} e.key or something close if not supported
  135. */
  136. getKeyboardKey: function (e) {
  137. if (typeof e.key === "string") {
  138. return e.key;
  139. }
  140. if (e.type === "keypress" && (
  141. (e.which >= 32 && e.which <= 126) ||
  142. (e.which >= 160 && e.which <= 255) )) {
  143. return String.fromCharCode(e.which);
  144. }
  145. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  146. switch (e.which) {
  147. case 27:
  148. return "Escape";
  149. case 191:
  150. return e.shiftKey ? "?" : "/";
  151. }
  152. if (e.shiftKey || e.type === "keypress") {
  153. return String.fromCharCode(e.which);
  154. } else {
  155. return String.fromCharCode(e.which).toLowerCase();
  156. }
  157. }
  158. };
  159. module.exports = KeyboardShortcut;