Fix Outlook Mac Hotkeys Hijacking

Disables Outlook from highjacking Mac keyboard shortcuts

  1. // ==UserScript==
  2. // @name Fix Outlook Mac Hotkeys Hijacking
  3. // @description Disables Outlook from highjacking Mac keyboard shortcuts
  4. // @copyright 2024, phonique (https://github.com/phonique/userscript-stop-outlook-hijack/)
  5. // @homepageURL https://github.com/phonique/userscript-stop-outlook-hijack
  6. // @version 0.1.0
  7. // @run-at document-start
  8. // @match https://outlook.office.com/*
  9. // @match https://outlook.office365.com.mcas.ms/mail/*
  10. // @grant none
  11. // @license MPL-2.0
  12. // @namespace https://greasyfork.org/users/1381446
  13. // ==/UserScript==
  14.  
  15. // event codes for arrow keys.
  16. var eventCodes = ["ArrowLeft","ArrowRight","ArrowUp","ArrowDown"];
  17.  
  18. // Possible events: keypress, keyup, keydown
  19. // currently Outlook seems to only require keydown and keyup.
  20. // Only `keypress` is not enough to disable Alt-key hijacking.
  21.  
  22.  
  23. // We enable useCapture here, because we want to capture the event while
  24. // it's trickling down the DOM and then ignore other Listeners that hijack
  25. // our default Alt functionality with our cancelBubble() and
  26. // stopImmediatePropagation() calls.
  27.  
  28. window.addEventListener('keydown', handler, true); // useCapture true, i.e. we are capturing the event while it's trickling down
  29. window.addEventListener('keyup', handler, true); // useCapture true
  30. // window.addEventListener('keypress', handler, true); // we don't currently need this.
  31.  
  32. function handler (e) {
  33. // alert("eventCode: " + e.code + " eventKey: " + e.key); // debug here
  34.  
  35. // If you want to completely disable Alt hijacking, not just in textboxes
  36. // uncomment the next and also comment out (or remove) the line following it.
  37. // if (e.code === "AltRight" || e.code === "AltLeft" || e.key === "Alt") {
  38. if ((e.code === "AltRight" || e.code === "AltLeft" || e.key === "Alt") && typeof(document.activeElement.role) !== 'undefined' && document.activeElement.role == 'textbox') {
  39. e.cancelBubble = true;
  40. e.stopImmediatePropagation();
  41. }
  42. /* // including previous UX-fixes
  43. if (eventCodes.indexOf(e.code) != -1 && e.shiftKey && e.altKey) {
  44. e.cancelBubble = true;
  45. e.stopImmediatePropagation();
  46. }
  47. if (eventCodes.indexOf(e.code) != -1 && e.altKey) {
  48. e.cancelBubble = true;
  49. e.stopImmediatePropagation();
  50. }
  51. if (eventCodes.indexOf(e.code) != -1 && e.shiftKey && e.metaKey) {
  52. e.cancelBubble = true;
  53. e.stopImmediatePropagation();
  54. }
  55. if (eventCodes.indexOf(e.code) != -1 && e.shiftKey && e.ctrlKey) {
  56. e.cancelBubble = true;
  57. e.stopImmediatePropagation();
  58. }
  59. */
  60. }