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.

utils.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Prefer keyboard handling of these elements over global shortcuts.
  3. * If a button is triggered using the Spacebar it should not trigger PTT.
  4. * If an input element is focused and M is pressed it should not mute audio.
  5. */
  6. const _elementsBlacklist = [
  7. 'input',
  8. 'textarea',
  9. 'button',
  10. '[role=button]',
  11. '[role=menuitem]',
  12. '[role=radio]',
  13. '[role=tab]',
  14. '[role=option]',
  15. '[role=switch]',
  16. '[role=range]',
  17. '[role=log]'
  18. ];
  19. /**
  20. * Returns the currently focused element if it is not blacklisted.
  21. *
  22. * @returns {HTMLElement|null} - The currently focused element.
  23. */
  24. export const getPriorityFocusedElement = (): HTMLElement | null =>
  25. document.querySelector(`:focus:is(${_elementsBlacklist.join(',')})`);
  26. /**
  27. * Returns the keyboard key from a KeyboardEvent.
  28. *
  29. * @param {KeyboardEvent} e - The KeyboardEvent.
  30. * @returns {string} - The keyboard key.
  31. */
  32. export const getKeyboardKey = (e: KeyboardEvent): string => {
  33. // @ts-ignore
  34. const { altKey, code, key, shiftKey, type, which } = e;
  35. // If alt is pressed a different char can be returned so this takes
  36. // the char from the code. It also prefixes with a colon to differentiate
  37. // alt combo from simple keypress.
  38. if (altKey) {
  39. const replacedKey = code.replace('Key', '');
  40. return `:${replacedKey}`;
  41. }
  42. // If e.key is a string, then it is assumed it already plainly states
  43. // the key pressed. This may not be true in all cases, such as with Edge
  44. // and "?", when the browser cannot properly map a key press event to a
  45. // keyboard key. To be safe, when a key is "Unidentified" it must be
  46. // further analyzed by jitsi to a key using e.which.
  47. if (typeof key === 'string' && key !== 'Unidentified') {
  48. return key;
  49. }
  50. if (type === 'keypress'
  51. && ((which >= 32 && which <= 126)
  52. || (which >= 160 && which <= 255))) {
  53. return String.fromCharCode(which);
  54. }
  55. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  56. switch (which) {
  57. case 27:
  58. return 'Escape';
  59. case 191:
  60. return shiftKey ? '?' : '/';
  61. }
  62. if (shiftKey || type === 'keypress') {
  63. return String.fromCharCode(which);
  64. }
  65. return String.fromCharCode(which).toLowerCase();
  66. };