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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. },
  69. /**
  70. * Registers a new shortcut.
  71. *
  72. * @param shortcutChar the shortcut character triggering the action
  73. * @param shortcutAttr the "shortcut" html element attribute mappring an
  74. * element to this shortcut and used to show the shortcut character on the
  75. * element tooltip
  76. * @param exec the function to be executed when the shortcut is pressed
  77. * @param helpDescription the description of the shortcut that would appear
  78. * in the help menu
  79. */
  80. registerShortcut: function( shortcutChar,
  81. shortcutAttr,
  82. exec,
  83. helpDescription) {
  84. _shortcuts[shortcutChar] = {
  85. character: shortcutChar,
  86. shortcutAttr: shortcutAttr,
  87. function: exec
  88. };
  89. if (helpDescription)
  90. this._addShortcutToHelp(shortcutChar, helpDescription);
  91. },
  92. /**
  93. * Unregisters a shortcut.
  94. *
  95. * @param shortcutChar unregisters the given shortcut, which means it will
  96. * no longer be usable
  97. */
  98. unregisterShortcut: function(shortcutChar) {
  99. _shortcuts.remove(shortcutChar);
  100. this._removeShortcutFromHelp(shortcutChar);
  101. },
  102. /**
  103. * Returns the tooltip string for the given shortcut attribute.
  104. *
  105. * @param shortcutAttr indicates the popover associated with the shortcut
  106. * @returns {string} the tooltip string to add to the given shortcut popover
  107. * or an empty string if the shortcutAttr is null, an empty string or not
  108. * found in the shortcut mapping
  109. */
  110. getShortcutTooltip: function (shortcutAttr) {
  111. if (typeof shortcutAttr === "string" && shortcutAttr.length > 0) {
  112. for (var key in _shortcuts) {
  113. if (_shortcuts.hasOwnProperty(key)
  114. && _shortcuts[key].shortcutAttr
  115. && _shortcuts[key].shortcutAttr === shortcutAttr) {
  116. return " (" + _shortcuts[key].character + ")";
  117. }
  118. }
  119. }
  120. return "";
  121. },
  122. /**
  123. * @param e a KeyboardEvent
  124. * @returns {string} e.key or something close if not supported
  125. */
  126. _getKeyboardKey: function (e) {
  127. if (typeof e.key === "string") {
  128. return e.key;
  129. }
  130. if (e.type === "keypress" && (
  131. (e.which >= 32 && e.which <= 126) ||
  132. (e.which >= 160 && e.which <= 255) )) {
  133. return String.fromCharCode(e.which);
  134. }
  135. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  136. switch (e.which) {
  137. case 27:
  138. return "Escape";
  139. case 191:
  140. return e.shiftKey ? "?" : "/";
  141. }
  142. if (e.shiftKey || e.type === "keypress") {
  143. return String.fromCharCode(e.which);
  144. } else {
  145. return String.fromCharCode(e.which).toLowerCase();
  146. }
  147. },
  148. /**
  149. * Adds the given shortcut to the help dialog.
  150. *
  151. * @param shortcutChar the shortcut character
  152. * @param shortcutDescriptionKey the description of the shortcut
  153. * @private
  154. */
  155. _addShortcutToHelp: function (shortcutChar, shortcutDescriptionKey) {
  156. var listElement = document.createElement("li");
  157. listElement.id = shortcutChar;
  158. var spanElement = document.createElement("span");
  159. spanElement.className = "item-action";
  160. var kbdElement = document.createElement("kbd");
  161. kbdElement.className = "regular-key";
  162. kbdElement.innerHTML = shortcutChar;
  163. spanElement.appendChild(kbdElement);
  164. var descriptionElement = document.createElement("span");
  165. descriptionElement.className = "item-description";
  166. descriptionElement.setAttribute("data-i18n", shortcutDescriptionKey);
  167. descriptionElement.innerHTML
  168. = APP.translation.translateString(shortcutDescriptionKey);
  169. listElement.appendChild(spanElement);
  170. listElement.appendChild(descriptionElement);
  171. var parentListElement
  172. = document.getElementById("keyboard-shortcuts-list");
  173. if (parentListElement)
  174. parentListElement.appendChild(listElement);
  175. },
  176. /**
  177. * Removes the list element corresponding to the given shortcut from the
  178. * help dialog
  179. * @private
  180. */
  181. _removeShortcutFromHelp: function (shortcutChar) {
  182. var parentListElement
  183. = document.getElementById("keyboard-shortcuts-list");
  184. var shortcutElement = document.getElementById(shortcutChar);
  185. if (shortcutElement)
  186. parentListElement.removeChild(shortcutElement);
  187. }
  188. };
  189. module.exports = KeyboardShortcut;