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

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