Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

keyboardshortcut.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* global APP, $, interfaceConfig */
  2. import Logger from 'jitsi-meet-logger';
  3. import {
  4. ACTION_SHORTCUT_PRESSED as PRESSED,
  5. ACTION_SHORTCUT_RELEASED as RELEASED,
  6. createShortcutEvent,
  7. sendAnalytics
  8. } from '../../react/features/analytics';
  9. import { toggleDialog } from '../../react/features/base/dialog';
  10. import { KeyboardShortcutsDialog }
  11. from '../../react/features/keyboard-shortcuts';
  12. import { SpeakerStats } from '../../react/features/speaker-stats';
  13. const logger = Logger.getLogger(__filename);
  14. /**
  15. * Map of shortcuts. When a shortcut is registered it enters the mapping.
  16. * @type {Map}
  17. */
  18. const _shortcuts = new Map();
  19. /**
  20. * Map of registered keyboard keys and translation keys describing the
  21. * action performed by the key.
  22. * @type {Map}
  23. */
  24. const _shortcutsHelp = new Map();
  25. /**
  26. * True if the keyboard shortcuts are enabled and false if not.
  27. * @type {boolean}
  28. */
  29. let enabled = true;
  30. /**
  31. * Maps keycode to character, id of popover for given function and function.
  32. */
  33. const KeyboardShortcut = {
  34. init() {
  35. this._initGlobalShortcuts();
  36. window.onkeyup = e => {
  37. if (!enabled) {
  38. return;
  39. }
  40. const key = this._getKeyboardKey(e).toUpperCase();
  41. const num = parseInt(key, 10);
  42. if (!($(':focus').is('input[type=text]')
  43. || $(':focus').is('input[type=password]')
  44. || $(':focus').is('textarea'))) {
  45. if (_shortcuts.has(key)) {
  46. _shortcuts.get(key).function(e);
  47. } else if (!isNaN(num) && num >= 0 && num <= 9) {
  48. APP.UI.clickOnVideo(num);
  49. }
  50. }
  51. };
  52. window.onkeydown = e => {
  53. if (!enabled) {
  54. return;
  55. }
  56. if (!($(':focus').is('input[type=text]')
  57. || $(':focus').is('input[type=password]')
  58. || $(':focus').is('textarea'))) {
  59. if (this._getKeyboardKey(e).toUpperCase() === ' ') {
  60. if (APP.conference.isLocalAudioMuted()) {
  61. sendAnalytics(createShortcutEvent(
  62. 'push.to.talk',
  63. PRESSED));
  64. logger.log('Talk shortcut pressed');
  65. APP.conference.muteAudio(false);
  66. }
  67. }
  68. }
  69. };
  70. },
  71. /**
  72. * Enables/Disables the keyboard shortcuts.
  73. * @param {boolean} value - the new value.
  74. */
  75. enable(value) {
  76. enabled = value;
  77. },
  78. /**
  79. * Opens the {@KeyboardShortcutsDialog} dialog.
  80. *
  81. * @returns {void}
  82. */
  83. openDialog() {
  84. APP.store.dispatch(toggleDialog(KeyboardShortcutsDialog, {
  85. shortcutDescriptions: _shortcutsHelp
  86. }));
  87. },
  88. /**
  89. * Registers a new shortcut.
  90. *
  91. * @param shortcutChar the shortcut character triggering the action
  92. * @param shortcutAttr the "shortcut" html element attribute mapping an
  93. * element to this shortcut and used to show the shortcut character on the
  94. * element tooltip
  95. * @param exec the function to be executed when the shortcut is pressed
  96. * @param helpDescription the description of the shortcut that would appear
  97. * in the help menu
  98. */
  99. registerShortcut(// eslint-disable-line max-params
  100. shortcutChar,
  101. shortcutAttr,
  102. exec,
  103. helpDescription) {
  104. _shortcuts.set(shortcutChar, {
  105. character: shortcutChar,
  106. function: exec,
  107. shortcutAttr
  108. });
  109. if (helpDescription) {
  110. this._addShortcutToHelp(shortcutChar, helpDescription);
  111. }
  112. },
  113. /**
  114. * Unregisters a shortcut.
  115. *
  116. * @param shortcutChar unregisters the given shortcut, which means it will
  117. * no longer be usable
  118. */
  119. unregisterShortcut(shortcutChar) {
  120. _shortcuts.delete(shortcutChar);
  121. _shortcutsHelp.delete(shortcutChar);
  122. },
  123. /**
  124. * @param e a KeyboardEvent
  125. * @returns {string} e.key or something close if not supported
  126. */
  127. _getKeyboardKey(e) {
  128. // If e.key is a string, then it is assumed it already plainly states
  129. // the key pressed. This may not be true in all cases, such as with Edge
  130. // and "?", when the browser cannot properly map a key press event to a
  131. // keyboard key. To be safe, when a key is "Unidentified" it must be
  132. // further analyzed by jitsi to a key using e.which.
  133. if (typeof e.key === 'string' && e.key !== 'Unidentified') {
  134. return e.key;
  135. }
  136. if (e.type === 'keypress'
  137. && ((e.which >= 32 && e.which <= 126)
  138. || (e.which >= 160 && e.which <= 255))) {
  139. return String.fromCharCode(e.which);
  140. }
  141. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  142. switch (e.which) {
  143. case 27:
  144. return 'Escape';
  145. case 191:
  146. return e.shiftKey ? '?' : '/';
  147. }
  148. if (e.shiftKey || e.type === 'keypress') {
  149. return String.fromCharCode(e.which);
  150. }
  151. return String.fromCharCode(e.which).toLowerCase();
  152. },
  153. /**
  154. * Adds the given shortcut to the help dialog.
  155. *
  156. * @param shortcutChar the shortcut character
  157. * @param shortcutDescriptionKey the description of the shortcut
  158. * @private
  159. */
  160. _addShortcutToHelp(shortcutChar, shortcutDescriptionKey) {
  161. _shortcutsHelp.set(shortcutChar, shortcutDescriptionKey);
  162. },
  163. /**
  164. * Initialise global shortcuts.
  165. * Global shortcuts are shortcuts for features that don't have a button or
  166. * link associated with the action. In other words they represent actions
  167. * triggered _only_ with a shortcut.
  168. */
  169. _initGlobalShortcuts() {
  170. this.registerShortcut('?', null, () => {
  171. sendAnalytics(createShortcutEvent('help'));
  172. this.openDialog();
  173. }, 'keyboardShortcuts.toggleShortcuts');
  174. // register SPACE shortcut in two steps to insure visibility of help
  175. // message
  176. this.registerShortcut(' ', null, () => {
  177. sendAnalytics(createShortcutEvent('push.to.talk', RELEASED));
  178. logger.log('Talk shortcut released');
  179. APP.conference.muteAudio(true);
  180. });
  181. this._addShortcutToHelp('SPACE', 'keyboardShortcuts.pushToTalk');
  182. if (!interfaceConfig.filmStripOnly) {
  183. this.registerShortcut('T', null, () => {
  184. sendAnalytics(createShortcutEvent('speaker.stats'));
  185. APP.store.dispatch(toggleDialog(SpeakerStats, {
  186. conference: APP.conference
  187. }));
  188. }, 'keyboardShortcuts.showSpeakerStats');
  189. }
  190. /**
  191. * FIXME: Currently focus keys are directly implemented below in
  192. * onkeyup. They should be moved to the SmallVideo instead.
  193. */
  194. this._addShortcutToHelp('0', 'keyboardShortcuts.focusLocal');
  195. this._addShortcutToHelp('1-9', 'keyboardShortcuts.focusRemote');
  196. }
  197. };
  198. export default KeyboardShortcut;