Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

keyboardshortcut.js 7.5KB

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