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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* global APP, $, JitsiMeetJS */
  2. /**
  3. * Initialise global shortcuts.
  4. * Global shortcuts are shortcuts for features that don't have a button or
  5. * link associated with the action. In other words they represent actions
  6. * triggered _only_ with a shortcut.
  7. */
  8. function initGlobalShortcuts() {
  9. KeyboardShortcut.registerShortcut("ESCAPE", null, function() {
  10. APP.UI.showKeyboardShortcutsPanel(false);
  11. });
  12. KeyboardShortcut.registerShortcut("?", null, function() {
  13. JitsiMeetJS.analytics.sendEvent("shortcut.shortcut.help");
  14. APP.UI.toggleKeyboardShortcutsPanel();
  15. }, "keyboardShortcuts.toggleShortcuts");
  16. KeyboardShortcut.registerShortcut("T", null, function() {
  17. JitsiMeetJS.analytics.sendEvent("shortcut.talk.clicked");
  18. APP.conference.muteAudio(true);
  19. }, "keyboardShortcuts.pushToTalk");
  20. /**
  21. * FIXME: Currently focus keys are directly implemented below in onkeyup.
  22. * They should be moved to the SmallVideo instead.
  23. */
  24. KeyboardShortcut._addShortcutToHelp("0", "keyboardShortcuts.focusLocal");
  25. KeyboardShortcut._addShortcutToHelp("1-9", "keyboardShortcuts.focusRemote");
  26. }
  27. /**
  28. * Map of shortcuts. When a shortcut is registered it enters the mapping.
  29. * @type {{}}
  30. */
  31. let _shortcuts = {};
  32. /**
  33. * Maps keycode to character, id of popover for given function and function.
  34. */
  35. var KeyboardShortcut = {
  36. init: function () {
  37. initGlobalShortcuts();
  38. var self = this;
  39. window.onkeyup = function(e) {
  40. var key = self._getKeyboardKey(e).toUpperCase();
  41. var num = parseInt(key, 10);
  42. if(!($(":focus").is("input[type=text]") ||
  43. $(":focus").is("input[type=password]") ||
  44. $(":focus").is("textarea"))) {
  45. if (_shortcuts.hasOwnProperty(key)) {
  46. _shortcuts[key].function(e);
  47. }
  48. else if (!isNaN(num) && num >= 0 && num <= 9) {
  49. APP.UI.clickOnVideo(num + 1);
  50. }
  51. //esc while the smileys are visible hides them
  52. } else if (key === "ESCAPE" &&
  53. $('#smileysContainer').is(':visible')) {
  54. APP.UI.toggleSmileys();
  55. }
  56. };
  57. window.onkeydown = function(e) {
  58. if(!($(":focus").is("input[type=text]") ||
  59. $(":focus").is("input[type=password]") ||
  60. $(":focus").is("textarea"))) {
  61. var key = self._getKeyboardKey(e).toUpperCase();
  62. if(key === "T") {
  63. if(APP.conference.isLocalAudioMuted())
  64. APP.conference.muteAudio(false);
  65. }
  66. }
  67. };
  68. $('body').popover({ selector: '[data-toggle=popover]',
  69. trigger: 'click hover',
  70. content: function() {
  71. return this.getAttribute("content")
  72. + self._getShortcutTooltip(this.getAttribute("shortcut"));
  73. }
  74. });
  75. },
  76. /**
  77. * Registers a new shortcut.
  78. *
  79. * @param shortcutChar the shortcut character triggering the action
  80. * @param shortcutAttr the "shortcut" html element attribute mappring an
  81. * element to this shortcut and used to show the shortcut character on the
  82. * element tooltip
  83. * @param exec the function to be executed when the shortcut is pressed
  84. * @param helpDescription the description of the shortcut that would appear
  85. * in the help menu
  86. */
  87. registerShortcut: function( shortcutChar,
  88. shortcutAttr,
  89. exec,
  90. helpDescription) {
  91. _shortcuts[shortcutChar] = {
  92. character: shortcutChar,
  93. shortcutAttr: shortcutAttr,
  94. function: exec
  95. };
  96. if (helpDescription)
  97. this._addShortcutToHelp(shortcutChar, helpDescription);
  98. },
  99. /**
  100. * Unregisters a shortcut.
  101. *
  102. * @param shortcutChar unregisters the given shortcut, which means it will
  103. * no longer be usable
  104. */
  105. unregisterShortcut: function(shortcutChar) {
  106. _shortcuts.remove(shortcutChar);
  107. this._removeShortcutFromHelp(shortcutChar);
  108. },
  109. /**
  110. * Returns the tooltip string for the given shortcut attribute.
  111. *
  112. * @param shortcutAttr indicates the popover associated with the shortcut
  113. * @returns {string} the tooltip string to add to the given shortcut popover
  114. * or an empty string if the shortcutAttr is null, an empty string or not
  115. * found in the shortcut mapping
  116. */
  117. _getShortcutTooltip: function (shortcutAttr) {
  118. if (typeof shortcutAttr === "string" && shortcutAttr.length > 0) {
  119. for (var key in _shortcuts) {
  120. if (_shortcuts.hasOwnProperty(key)
  121. && _shortcuts[key].shortcutAttr
  122. && _shortcuts[key].shortcutAttr === shortcutAttr) {
  123. return " (" + _shortcuts[key].character + ")";
  124. }
  125. }
  126. }
  127. return "";
  128. },
  129. /**
  130. * @param e a KeyboardEvent
  131. * @returns {string} e.key or something close if not supported
  132. */
  133. _getKeyboardKey: function (e) {
  134. if (typeof e.key === "string") {
  135. return e.key;
  136. }
  137. if (e.type === "keypress" && (
  138. (e.which >= 32 && e.which <= 126) ||
  139. (e.which >= 160 && e.which <= 255) )) {
  140. return String.fromCharCode(e.which);
  141. }
  142. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  143. switch (e.which) {
  144. case 27:
  145. return "Escape";
  146. case 191:
  147. return e.shiftKey ? "?" : "/";
  148. }
  149. if (e.shiftKey || e.type === "keypress") {
  150. return String.fromCharCode(e.which);
  151. } else {
  152. return String.fromCharCode(e.which).toLowerCase();
  153. }
  154. },
  155. /**
  156. * Adds the given shortcut to the help dialog.
  157. *
  158. * @param shortcutChar the shortcut character
  159. * @param shortcutDescriptionKey the description of the shortcut
  160. * @private
  161. */
  162. _addShortcutToHelp: function (shortcutChar, shortcutDescriptionKey) {
  163. var listElement = document.createElement("li");
  164. listElement.id = shortcutChar;
  165. var spanElement = document.createElement("span");
  166. spanElement.className = "item-action";
  167. var kbdElement = document.createElement("kbd");
  168. kbdElement.className = "regular-key";
  169. kbdElement.innerHTML = shortcutChar;
  170. spanElement.appendChild(kbdElement);
  171. var descriptionElement = document.createElement("span");
  172. descriptionElement.className = "item-description";
  173. descriptionElement.setAttribute("data-i18n", shortcutDescriptionKey);
  174. descriptionElement.innerHTML
  175. = APP.translation.translateString(shortcutDescriptionKey);
  176. listElement.appendChild(spanElement);
  177. listElement.appendChild(descriptionElement);
  178. var parentListElement
  179. = document.getElementById("keyboard-shortcuts-list");
  180. if (parentListElement)
  181. parentListElement.appendChild(listElement);
  182. },
  183. /**
  184. * Removes the list element corresponding to the given shortcut from the
  185. * help dialog
  186. * @private
  187. */
  188. _removeShortcutFromHelp: function (shortcutChar) {
  189. var parentListElement
  190. = document.getElementById("keyboard-shortcuts-list");
  191. var shortcutElement = document.getElementById(shortcutChar);
  192. if (shortcutElement)
  193. parentListElement.removeChild(shortcutElement);
  194. }
  195. };
  196. module.exports = KeyboardShortcut;