您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

keyboardshortcut.js 7.0KB

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