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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, ctrlKey } = 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. if (ctrlKey) {
  49. return `-${key}`;
  50. }
  51. return key;
  52. }
  53. if (type === 'keypress'
  54. && ((which >= 32 && which <= 126)
  55. || (which >= 160 && which <= 255))) {
  56. return String.fromCharCode(which);
  57. }
  58. // try to fallback (0-9A-Za-z and QWERTY keyboard)
  59. switch (which) {
  60. case 27:
  61. return 'Escape';
  62. case 191:
  63. return shiftKey ? '?' : '/';
  64. }
  65. if (shiftKey || type === 'keypress') {
  66. return String.fromCharCode(which);
  67. }
  68. return String.fromCharCode(which).toLowerCase();
  69. };